【问题标题】:jQuery-File-Upload done callbackjQuery-File-Upload 完成回调
【发布时间】:2015-02-09 13:37:39
【问题描述】:

我正在尝试使用 blueimp 的文件上传,并且 - 像很多人一样 - 我不明白为什么 done 回调不起作用!

即使失败回调发送失败问题,文件上传也能正常工作(我使用 php 上传处理程序)。我已经阅读了很多关于 json 问题的主题,但没有适合我的问题的答案。

这是我的 javascript 代码:

$(function () {
'use strict';
var url = 'server/php/';
$('#fileupload').fileupload({
    url: url,
    dataType: 'json',
    done: function (e, data) {
        $.each(data.result.files, function (index, file) {
            $('<p/>').text(file.name).appendTo('#files');
        });
    },
    fail: function (data) {
        alert("Fail!");
    },
    progressall: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .progress-bar').css(
            'width',
            progress + '%'
        );
    }
}).prop('disabled', !$.support.fileInput)
    .parent().addClass($.support.fileInput ? undefined : 'disabled');
});

您有解决方案或进行测试以发现一些错误吗?

EDIT1:成功回调仅在我使用dataType: 'text'时有效

EDIT2:这是触发完成回调时的响应:

{"readyState":4,"responseText":"
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in       /home/mesdevis/work/SITE/acces_web/inner/upload- image/server/php/UploadHandler.php on line 299
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 780
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 804
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in  /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 806
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 809
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 812
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 815
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 819
\n
 \nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 822
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 826
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 832
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 885
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 905
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 963
\n
\nWarning: Unexpected character in input: '\\' (ASCII=92) state=1 in /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 1048
\n
\nWarning: Cannot modify header information - headers already sent by (output started at /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php:299) in  /home/mesdevis/work/SITE/acces_web/inner/upload-image/server/php/UploadHandler.php on line 1120
\n{\"files\":    [{\"name\":\"IMG_0060.jpg\",\"size\":55277,\"type\":\"image\\/jpeg\",\"url\":\"http:\\/\\/wilfried@work.wilfryed.com\\/SITE\\/acces_web\\/inner\\/upload-image\\/server\\/php\\/files\\/IMG_0060.jpg\",\"thumbnailUrl\":\"http:\\/\\/wilfried@work.wilfryed.com\\/SITE\\/acces_web\\/inner\\/upload-image\\/server\\/php\\/files\\/thumbnail\\/IMG_0060.jpg\",\"deleteUrl\":\"http:\\/\\/wilfried@work.wilfryed.com\\/SITE\\/acces_web\\/inner\\/upload-image\\/server\\/php\\/?file=IMG_0060.jpg\",\"deleteType\":\"DELETE\"}]}","status":200,"statusText":"OK"}

【问题讨论】:

    标签: jquery file-upload jquery-callback


    【解决方案1】:

    删除“dataType: 'json'”行,并在“done”回调中将“data.result.files”更改为“data.files”即可!

    dataType: 'json',
            done: function (e, data) {
                $.each(data.files, function (index, file) {
                    $('<p/>').text(file.name).appendTo('#files');
                });
            },
    

    【讨论】:

      【解决方案2】:

      尝试将完成移动到通话结束

      $.ajax({url: '/'}).done(function(data) {});
      

      对于您的代码,它将类似于:

      $('#fileupload').fileupload({
          url: url,
          dataType: 'json',
          success: function (data) {
              alert("Success!");
          },
          progressall: function (e, data) {
              var progress = parseInt(data.loaded / data.total * 100, 10);
              $('#progress .progress-bar').css(
                  'width',
                  progress + '%'
              );
          }
      })
      .done(function (e, data) {
              $.each(data.result.files, function (index, file) {
                  $('<p/>').text(file.name).appendTo('#files');
              });
          )
      .prop('disabled', !$.support.fileInput)
          .parent().addClass($.support.fileInput ? undefined : 'disabled');
      });
      

      【讨论】:

      • 感谢您的帮助,但没有效果。
      猜你喜欢
      • 2013-10-18
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2011-09-10
      相关资源
      最近更新 更多