上传前如何在内存中操作文件流

How to manipulate filestream in memory before uploading

我正在开发一个上传器,它需要在上传数据之前通过将它们覆盖为预定值来审查上传数据中的几个字节。

现在,我已经连接到 onSubmit 事件,因为它允许执行非阻塞工作。下面你可以看到我的事件处理程序。在调用 promise.success(); 之前,您会注意到评论较多的部分,这是我需要帮助解决的问题。我怎样才能 return/set 那里的字节数组?

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;
},