[英]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.