jQuery-File-Upload: Handle server errors

Ben picture Ben · Apr 11, 2014 · Viewed 6.9k times · Source

I'm trying to implement error handling for blueimp's jQuery-File-Upload. It's easy to implement for errors which i can catch on the server an wrap it into a JSON-object. But what if the server has a PHP-error and the message is displayed as standard-PHP-message? I tried to handle it with the fail-callback:

jQuery('#fileupload').fileupload({
    fail: function (ev, data) {
        if (data.jqXHR) {
            alert('Server-error:\n\n' + data.jqXHR.responseText); 
        }
    }, 
    otherOptions
});

It's working, but the callback is also fired if I press the cancel-button to remove an image. So I added the if with the data.jqXHR to differ between a server error and a cancel-button-press. But then, if cancel-button is pressed, image isn't removed from the list anymore.

Any idea's how to implement such an error handling for unexpected server errors?

Thanks, Ben

Answer

Ben picture Ben · Apr 12, 2014

I found the answer. I needed to use

.bind('fileuploadfail', function (e, data) {

instead of overwriting the fail-function directly.