javascript - 上传前如何操作内存中的文件流

标签 javascript azure fine-uploader fileapi

我正在开发一个上传程序,需要在上传数据之前将其覆盖为预定值来审查上传数据中的一些字节。

现在,我已连接到 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/

相关文章:

javascript - 单个文件上传的几个实例(使用fine-uploader)

fine-uploader - FineUploader 将多个文件作为一个请求上传

javascript - 精细上传到 S3 文件夹

javascript - 如何从变量构建正则表达式

azure - 使用 Azure 逻辑应用访问始终加密的数据

javascript - Yii 客户端验证不适用于 ajax 加载的表单

Azure Data Lake Gen 2 默认访问控制列表未应用于新文件

azure - 将 Azure SQL 数据库的备份还原到另一个资源组的另一个 Azure SQL 数据库

javascript - 如何通过鼠标悬停来制作图案

javascript - Angular $http - 在接收响应数据时处理它