[英]jQuery-File-Upload where is the file data when pre processing? http://blueimp.github.io/jQuery-File-Upload/
我开始使用优秀的 http://blueimp.github.io/jQuery-File-Upload/文件上传项目。
从文档的文件处理选项部分看来,jquery.fileupload-process.js似乎可以解析甚至修改文件的二进制数据(文件数组 - 应用了进程的结果,原始文件包含原始上传的文件)
(解析,附加或加密或对其做某事)
但对于我的生活,我似乎无法弄清楚数组中的实际文件数据在哪里,以便我可以在上传之前对其进行预处理。
数据数组的哪一部分有“something.pdf”二进制文件? 这样我才能在上传之前解析并转换它?
//FROM: jquery.fileupload-process.js
//The list of processing actions:
processQueue: [
{
action: 'log'
}
],
add: function (e, data) {
var $this = $(this);
data.process(function () {
return $this.fileupload('process', data);
});
originalAdd.call(this, e, data);
}
},
processActions: {
log: function (data, options) {
console.log(data.files[0]); //Is it here?
console.log(data); //Is it here?
console.log(data.files[data.index]); //Is it here?
console.log(data.files[data.index].name); //Is it here?
//where?
谢谢。
访问当前处理的文件的正确方法如下:
var file = data.files[data.index];
对于支持File API的浏览器,这是一个File对象。
要检索实际的File数据,我们必须使用FileReader接口:
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);
在@ Sebastian的答案的基础上,解释在哪里放置FileReader以使用fileupload插件可能很有用。 (我本来是对@ Sebastian的回答发表评论,但在评论栏中没有空格)。
我已将此内容构建到流程操作中(使用readAsText
而不是readAsArrayBuffer
),如下所示:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.