Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a JavaScript Object into an actual file in order to upload with HTML5

Tags:

javascript

I have a JavaScript object with a huge amount of data in it, including several large base64 encoded strings.

We are currently sending the data to the server via a simple ajax POST but since the data is so large the wait time for the user is unacceptable.

For this reason we wish to harness the new html5 file upload features and actually measure the progress as the data is uploaded to the server so that the user is provided with constant feedback during this lengthy process.

In order to use this feature this large array will have to be sent as an actual file rather than as a huge object sent as url params.

Is there any way to either:

A. Convert this object into an actual text file and send it that way.

or

B. Hook into the html5 progress api and actually measure the progress of this standard ajax POST.

Thanks in advance.

like image 333
gordyr Avatar asked Jan 03 '13 22:01

gordyr


People also ask

How to convert a string to an object in JavaScript?

The only native Javascript function to convert a string into an object is JSON.parse (). For example, var parsed = JSON.parse (' {"foo":"bar"}'). To convert strings of other formats, it has to be done manually. That covers the basics, but let us walk through a few more examples in this guide – Read on!

How to upload a file using HTML?

The same is true for uploading files, we only need three steps: In HTML, we can use the input element. Just set the type of this element to file, then the element can be used to select the file. After the user selects a file, the metadata of the file will be stored in the files property of this input element.

How to turn an array into a string in JavaScript?

Simply put, turning an array or object in a string. The JSON.stringify (OBJECT) will turn an object into a JSON encoded string, then we use the JSON.parse (STRING) function to turn it back into an object. 2) MANUAL FOR LOOP 2-for.js

How to upload a directory to the form data?

Now let’s look at the steps to upload a directory. Similarly to before, we need to set the attribute of the input element to this: Then when uploading the directory, the files object of input element will have the webkitRlativePath property, and we will also add them to the formdata


1 Answers

It's possible to take a JavaScript object (myData), stringify it into JSON, pack that into a Blob of mimetype JSON, and send that to the server with the HTML5 upload API. You can use the progress (in the progress callback function) to update the value of an HTML5 progress bar.

var myData = {
    data1: "Huge amount of data",
    data2: "More very large data"
};

var xhr = new XMLHttpRequest();

xhr.upload.addEventListener('progress', function (e) {
    console.log(100*(e.loaded / e.total) + '%');
}, false);

xhr.open('POST', 'url', true);

var data = new FormData();
data.append('file', new Blob([JSON.stringify(myData)],{type:'application/json'}));
xhr.send(data);
like image 145
fuzic Avatar answered Oct 20 '22 15:10

fuzic