jQuery-File-Upload where is the file data when pre processing? http://blueimp.github.io/jQuery-File-Upload/

被刻印的时光 ゝ 提交于 2019-12-06 11:02:57

The correct way to access the currently processed file is the following:

var file = data.files[data.index];

For browsers which support the File API, this is a File object.

To retrieve the actual File data, we have to use the FileReader interface:

var fileReader = new FileReader();
fileReader.onload = function (event) {
    var buffer = event.target.result;
    // TODO: Do something with the ArrayBuffer containing the file's data
};
fileReader.readAsArrayBuffer(file);

It may be useful, building on @Sebastian's answer, to explain where to put the FileReader, to work with the fileupload plugin. (I would have made this a comment on @Sebastian's answer, but didn't have space in the comment field).

I have built this in to the process action (using readAsText instead of readAsArrayBuffer) as follows:

alertText: function(data, options) {
         var dfd = $.Deferred(),
            file = data.files[data.index];
        var fileReader = new FileReader();
        fileReader.onload = function (event) {
            var fileText = event.target.result;
            alert('Text of uploaded file: '+ fileText);
       }; 
        fileReader.readAsText(file);
        return dfd.promise();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!