I use event.clipboardData
to get image from clipboard, and then upload it server, code:
var items = e.clipboardData.items; for(var i=0;i<items.length;i++) { if(items[i].type.indexOf("image")!=-1) { var blob=items[i].getAsFile(); var data = new FormData(); data.append("ImageFileField",blob); _post2("url...",data); } }
NOTE: _post2()
is a function using XMLHttpRequest
to do upload works.
Above code work fine, the image from clipboard can upload to my server directly.
BUT I found a problem, the filename of image upload to server is fixed as "blob", can I modify the filename before upload to server?
This is the upload data detail:
Request Payload
------WebKitFormBoundaryW0NQVOkdrfkYGWV3
Content-Disposition: form-data; name="%%File.48257279001171c9.2c36671da7f1b6c9482575de002e1f14.$Body.0.3D8"; filename="blob"Content-Type: image/png
------WebKitFormBoundaryW0NQVOkdrfkYGWV3--
When you are using Google Chrome you can use/abuse the Google Filesystem API for this. Here you can create a file with a specified name and write the content of a blob to it. Then you can return the result to the user.
To construct a Blob from other non-blob objects and data, use the Blob() constructor. To create a blob that contains a subset of another blob's data, use the slice() method. To obtain a Blob object for a file on the user's file system, see the File documentation.
The blob data is stored in the memory or filesystem of a user depending on the browser features and size of the blob. A simple blob can be used anywhere we wish just like files. The content of the blob can easily be read as ArrayBuffer which makes blobs very convenient to store the binary data. let res = def.
According to FormData, you should be able to add a filename parameter to your data.append()
call as follows:
data.append("ImageFileField", blob, "imageFilename.png");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With