我正在开发一个上传程序,需要在上传数据之前将其覆盖为预定值来审查上传数据中的一些字节。
现在,我已连接到 onSubmit
事件,因为它允许执行非阻塞工作。您可以在下面看到我的事件处理程序。在调用 promise.success();
之前,您会注意到大量注释的部分,这就是我需要帮助的问题。如何在那里返回/设置字节数组?
onSubmit: function (id, name) {
// http://docs.fineuploader.com/branch/master/api/events.html#submit
// Called when the item has been selected and is a candidate for uploading.
// Return false to prevent submission to the 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 to return manipulated byteArray?)
/******************* Missing part...**********************/
// signal success
promise.success();
}
// initiate async work
var file = this.getFile(id);
reader.readAsArrayBuffer(file);
// return promise
return promise;
},
最佳答案
我明白了。这是关键部分:
if(needsManipulation(byteArray))
{
manipulateByteArray(byteArray);
// construct a new blob
var newBlob = { blob: new Blob([byteArray], { type: 'application/octet-stream' }), name: name };
// restart the process for the adjusted file
uploader.addFiles(newBlob);
// signal failure and exit early
promise.failure();
return;
}
这是修改后的代码:
onSubmit: function (id, name) {
// http://docs.fineuploader.com/branch/master/api/events.html#submit
// Called when the item has been selected and is a candidate for uploading.
// Return false to prevent submission to the uploader.
// create promise
var promise = new qq.Promise();
// add uploader instance to 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 a new blob
var newBlob = { blob: new Blob([byteArray], { type: 'application/octet-stream' }), name: name };
// restart the process for the adjusted file
uploader.addFiles(newBlob);
// signal failure and exit early
promise.failure();
return;
}
// signal success
promise.success();
}
// initiate async reading work
var file = this.getFile(id);
reader.readAsArrayBuffer(file);
// return promise
return promise;
},
关于javascript - 上传前如何操作内存中的文件流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29945565/