I am using the HTML5 file API to build multi-page form data to submit XHR to a web service. I have everything that works in FF, which has a convenient convenient getAsBinary () method included in their implementation of the API files. It was a pretty nice deal. He basically went:
var const;
Worked like a charm.
To make it work in Chrome, I have to create a FileReader object that processes the bit differently. I essentially have to go:
var const; // constructor const += headers; const += field_data; var reader = new FileReader(); for(var i = 0; i < files.length; i++) { reader.onload = (function(file) { const += file.target.result; // const is not in scope in this anonymous function! } reader.readAsBinaryString(files[i]); } sendData(const);
Which does not work for two main reasons. Firstly, reading occurs asynchronously, therefore, by the time the sendData () function is received, the file data is not written to the constant variable. Secondly, the const variable goes beyond the scope inside the reader.onload handler. However, I am rewriting the code, I seem to encounter one of these obstacles, and I am struggling to come up with an elegant way to handle it.
Any suggestions?
source share