2015-02-09 73 views
1

我正在嘗試使用blueimp的文件上傳功能,並且像很多人一樣 - 我無法弄清楚爲什麼完成的回調不起作用!jQuery-File-Upload 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:\\/\\/[email protected]\\/SITE\\/acces_web\\/inner\\/upload-image\\/server\\/php\\/files\\/IMG_0060.jpg\",\"thumbnailUrl\":\"http:\\/\\/[email protected]\\/SITE\\/acces_web\\/inner\\/upload-image\\/server\\/php\\/files\\/thumbnail\\/IMG_0060.jpg\",\"deleteUrl\":\"http:\\/\\/[email protected]\\/SITE\\/acces_web\\/inner\\/upload-image\\/server\\/php\\/?file=IMG_0060.jpg\",\"deleteType\":\"DELETE\"}]}","status":200,"statusText":"OK"} 

回答

1

通過「完成」回調中的「data.files」刪除行「dataType:'json',」並更改「data.result.files」,它可以工作!

dataType: 'json', 
     done: function (e, data) { 
      $.each(data.files, function (index, file) { 
       $('<p/>').text(file.name).appendTo('#files'); 
      }); 
     }, 
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'); 
}); 
+0

謝謝你的幫助,但那不是有效的。 – wilfryed 2015-02-09 13:52:11

相關問題