Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set file name in blob data when I upload data to server using html5?

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--

like image 632
user3135560 Avatar asked Dec 26 '13 03:12

user3135560


People also ask

How do you give a blob filename?

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.

How do I create a blob in HTML?

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.

How does Javascript handle BLOB data?

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.


1 Answers

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"); 
like image 96
cookch10msu Avatar answered Sep 26 '22 05:09

cookch10msu