Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Blob Upload with FormData

I am having a problem uploading a blob created in javascript to my server. The basic idea is that a user uploads an image and in javascript I center crop the image and downsample it before transmission.

The image manipulation is working fine, but the upload itself is not working right. Here is the code that does the upload and conversion from canvas to blob

function uploadCanvasData() {     var canvas = $('#ImageDisplay').get(0);     var dataUrl = canvas.toDataURL("image/jpeg");      var blob = dataURItoBlob(dataUrl);      var formData = new FormData();     formData.append("file", formData);      var request = new XMLHttpRequest();     request.onload = completeRequest;      request.open("POST", "IdentifyFood");     request.send(formData); }  function dataURItoBlob(dataURI) {     var byteString = atob(dataURI.split(',')[1]);      var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]      var ab = new ArrayBuffer(byteString.length);     var ia = new Uint8Array(ab);     for (var i = 0; i < byteString.length; i++)     {         ia[i] = byteString.charCodeAt(i);     }      var bb = new Blob([ab], { "type": mimeString });     return bb; } 

The server claims that no files were uploaded, and when I use chrome to examine the request, I see the request payload as:

------WebKitFormBoundaryyzYbm61DKgS09tpB Content-Disposition: form-data; name="file"  [object FormData] ------WebKitFormBoundaryyzYbm61DKgS09tpB-- 

In contrast to the payload of a form being submitted with input type="file"

------WebKitFormBoundaryUOn3WXb7pKLmOxRZ Content-Disposition: form-data; name="imagefile"; filename="-3YQHiVaGWo.jpg" Content-Type: image/jpeg   ------WebKitFormBoundaryUOn3WXb7pKLmOxRZ-- 

So it looks to me like the XMLHttpRequest is just uploading the result of calling blob.toString()

Does anyone know what I am doing wrong here? Is there a better approach I should be using?

like image 386
Max Ehrlich Avatar asked Aug 15 '13 13:08

Max Ehrlich


People also ask

How do I upload a file to formData?

You can upload the selected file by creating a FormData class and passing it to Axios' post() function. const input = document. querySelector('#my-input'); const formData = new FormData(); formData. append('myFile', input.

How do you send a file using multipart form data?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

What is blob in JavaScript?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.


1 Answers

You have a typo in the function uploadCanvasData it should read

formData.append("file", blob); 

Read your code more carefully!

like image 85
Max Ehrlich Avatar answered Sep 21 '22 14:09

Max Ehrlich