wilfryed
wilfryed

Reputation: 25

jQuery-File-Upload done callback

I'm trying to use blueimp's file upload, and -like a lot of people- I can't figure why the done callback doesn't work!

The file upload works well (I use the php upload handler) even if the fail callback send a fail issue. I've read a lot of topics about the json problems, but no answer fit my problem.

Here's my javascript code :

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

Do you have a solution or something to test to find some errors?

EDIT1: the success callback works only when I use dataType: 'text'

EDIT2: Here's is the response when the complete callback is triggered:

{"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"}

Upvotes: 2

Views: 12787

Answers (2)

Xxavier
Xxavier

Reputation: 46

Remove the line "dataType: 'json'," and change "data.result.files" by "data.files" in the "done" callback and it works !

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

Upvotes: 3

MrPickles
MrPickles

Reputation: 949

try moving the done to the end of the call

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

for your code it would be something like:

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

Upvotes: 0

Related Questions