javascript - How to manipulate filestream in memory before uploading -
i'm working on uploader needs censor few bytes in uploaded data overwriting them predetermined value, before uploading them.
right now, i've hooked onsubmit
event because allows non-blocking work performed. below can see event handler. before call promise.success();
note heavily commented part, problem need with. how can return/set byte array there?
onsubmit: function (id, name) { // http://docs.fineuploader.com/branch/master/api/events.html#submit // called when item has been selected , candidate uploading. // return false prevent submission uploader. // create promise var promise = new qq.promise(); // configure file reader var reader = new filereader(); reader.onerror = function (e) { promise.failure("error occured reading file"); }; reader.onabort = function (e) { promise.failure("file reading aborted"); }; reader.onload = function (e) { var buffer = reader.result; var bytearray = new uint8array(buffer); manipulatebytearray(bytearray); /******************* missing part... **********************/ // todo (how return manipulated bytearray?) /******************* missing part...**********************/ // signal success promise.success(); } // initiate async work var file = this.getfile(id); reader.readasarraybuffer(file); // return promise return promise; },
i figured out. crucial part:
if(needsmanipulation(bytearray)) { manipulatebytearray(bytearray); // construct new blob var newblob = { blob: new blob([bytearray], { type: 'application/octet-stream' }), name: name }; // restart process adjusted file uploader.addfiles(newblob); // signal failure , exit promise.failure(); return; }
here's revised code:
onsubmit: function (id, name) { // http://docs.fineuploader.com/branch/master/api/events.html#submit // called when item has been selected , candidate uploading. // return false prevent submission uploader. // create promise var promise = new qq.promise(); // add uploader instance closure var uploader = this; // configure file reader var reader = new filereader(); reader.onerror = function (e) { promise.failure("error occured reading file"); }; reader.onabort = function (e) { promise.failure("file reading aborted"); }; reader.onload = function (e) { var buffer = reader.result; var bytearray = new uint8array(buffer); if(needsmanipulation(bytearray)) { manipulatebytearray(bytearray); // construct new blob var newblob = { blob: new blob([bytearray], { type: 'application/octet-stream' }), name: name }; // restart process adjusted file uploader.addfiles(newblob); // signal failure , exit promise.failure(); return; } // signal success promise.success(); } // initiate async reading work var file = this.getfile(id); reader.readasarraybuffer(file); // return promise return promise; },
Comments
Post a Comment