【问题标题】:jQuery-File-Upload where is the file data when pre processing? http://blueimp.github.io/jQuery-File-Upload/jQuery-File-Upload 预处理时文件数据在哪里? http://blueimp.github.io/jQuery-File-Upload/
【发布时间】:2014-01-03 06:27:42
【问题描述】:

我已经开始玩 excellent http://blueimp.github.io/jQuery-File-Upload/ 文件上传项目了。

documentation 的文件处理选项部分看来,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?

谢谢。

【问题讨论】:

    标签: javascript jquery jquery-file-upload blueimp


    【解决方案1】:

    访问当前处理的文件的正确方法如下:

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

    对于支持File API 的浏览器,这是一个文件对象。

    要检索实际的文件数据,我们必须使用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);
    

    【讨论】:

      【解决方案2】:

      在@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();
          }
      

      【讨论】:

        猜你喜欢
        • 2014-06-06
        • 2014-05-28
        • 1970-01-01
        • 2014-08-26
        • 1970-01-01
        • 1970-01-01
        • 2015-03-04
        • 2015-03-06
        • 1970-01-01
        相关资源
        最近更新 更多