I’m tying to use the HTML5 FileReader and possibly the FileWriter API to manipulate and then upload the manipulated data from a file type input tag.
Here’s what I have so far:
$(function() {
$("#files").change(function(e) {
var files = e.target.files
, reader = new FileReader()
, i;
for(i=0; f = files[i]; ++i) {
f = doSomething(reader.readAsBinaryString(f)); // Manipulate the File
}
return true;
});
});
So how would I go about implementing the doSomething method? I need to return a FileObject that the form can then submit. Is this currently possible on any of the browsers?
Thank you.
There are a couple of things wrong with your current approach:
You don’t need the
FileWriterAPI. That cannot be used without the HTML5 Filesystem API. What you do need is theBlobBuilderAPI from the File API: Writer spec.Second, the
FileReaderread methods are asynchronous, so you can’t pass the resultto
doSomething()like you’re doing. You also need to create a separate reader object for each file.One thing you could try is to read the file as an
ArrayBuffer, manipulate its bytes via a JS typed array (Uint8Array), create a Blob from that buffer, then send the result to the server. Something like this might work (untested):I’m not sure if you need the mimetype passed into
bb.getBlob(), but that wouldbe the content type of the file you’re working with. Try it.