Alter file contents before uploading

2019-08-13 00:51发布

问题:

I would like to encrypt the contents of a file before uploading to our server .. we are using fineuploader.

What I would like to do is:

function onSubmitted(id)
{
    var file = uploader.getFile(id);
    var reader = new FileReader();

    reader.onload = function (e) {
      //encrypt here

      // but how do I save the file back to fineuploader?

      //then continue to submit/upload
     };

   reader.readAsArrayBuffer(file);
}

is there any way of doing this out of the box or will I need to resort to rampant hackery :) :)

回答1:

How about returning false to reject the file from onSubmit, encrypt it, then re-submit the encrypted version via the addBlobs API method. For example:

callbacks: {
    onSubmit: function(id) {
        if (!fileOrBlob.blob || !fileOrBlob.blob.encrypted) {
            var fileOrBlob = uploader.getFile(id),
                reader = new FileReader();

            reader.onload = function (e) {
                //encrypt here

                encryptedBlob.encrypted = true;
                uploader.addBlobs({name: fileOrBlob.name, blob: encryptedBlob});
            };

            reader.readAsArrayBuffer(file);

            return false;
        }
    }
} 

NOTE: The above has not been tested. If you run into serious issues, I'll check back in about 9 hours.