jQuery-File-Upload完成回调

时间:2015-02-09 13:37:40

标签: jquery file-upload jquery-callback

我试图使用blueimp的文件上传,并且像很多人一样 - 我无法理解为什么完成的回调无法正常工作!

即使失败回调发送失败问题,文件上传也能正常运行(我使用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"}

2 个答案:

答案 0 :(得分:2)

删除行&#34; dataType:&#39; json&#39;,&#34;并改变&#34; data.result.files&#34;通过&#34; data.files&#34;在&#34;完成&#34;回调,它的工作原理!

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

答案 1 :(得分:0)

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

$.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');
});