Uploading BLOB/ArrayBuffer with Dropzone.js

旧街凉风 提交于 2019-12-04 11:37:50

This is a late answer to my own question, but it is the solution we went for in the end.

We kept dropzone.js just for the nice interface and some help functions, but we decided to do the actual file upload using $.ajax().

We read the file as an array buffer using the HTML5 FileReader

var reader = new FileReader();
reader.onloadend = function(e) {
    resolve(e.target.result);
};
reader.onerror = function(e) {
    reject(e.target.error);
};
reader.readAsArrayBuffer(file);

and then pass it as the data argument in the ajax options.

I recently came across this exact issue and so did some investigation into the Dropzone library.

I managed to correct the upload process for SharePoint/Office 365 by monkey patching the Dropzone.prototype.submitRequest() method to modify it to use use my own getArrayBuffer method that returns an ArrayBuffer using the FileReader API before sending it via the Dropzone generated XMLHttpRequest.

This enables me to continue to use the features of Dropzone API.

Only tested on a single auto upload, so will need further investigation for multi file upload.

Monkey patch

Dropzone.prototype.submitRequest = function (xhr, formData, files) {
    getArrayBuffer(files[0]).then(function (buffer) {
        return xhr.send(buffer);
    });
};

getArrayBuffer

function getArrayBuffer(file) {
    return new Promise(function (resolve, reject) {
        var reader = new FileReader();

        reader.onloadend = function (e) {
            resolve(e.target.result);
        };
        reader.onerror = function (e) {
            reject(e.target.error);
        };
        reader.readAsArrayBuffer(file);
    });
}

After the file is uploaded into SharePoint, I use the Dropzone 'success' event to update the file with metadata.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!