Search code examples
jqueryiframecallbackjquery-file-upload

jQuery file upload plugin - fail callback not firing in IE < 10


I am using the https://github.com/blueimp/jQuery-File-Upload

It uses XHR file uploads in modern browser and hidden iframe uploads in oldIEs (8,9).

The server returns a 4xx status code (error/fail), when file validation failed.

$('#fileupload').fileupload({
    url: url,
    dataType: 'json',
    done: function (e, data) {
       console.log("done")
    },
    fail: function (e, data) {
       console.log("fail")
    }
})

However in oldIEs the done callback is called even when I send a 4xx status code. Is there a way to make it call the fail callback (https://github.com/blueimp/jQuery-File-Upload/wiki/Options#fail) and read the response body or statuscode?

I think it might have to do something with the hidden iframe method of uploading the files but I couldn't find anything specific in the docs.


Solution

  • I should have checked the Frequently Asked Questions

    // By default, the iFrame Transport handles all responses as success,
    // so we override the iFrame to JSON converter to handle error responses:
    
    $.ajaxSetup({
        converters: {
        'iframe json': function (iframe) {
            var result = iframe && $.parseJSON($(iframe[0].body).text());
            if (result.error) {
                // Handling JSON responses with error property:
                // {"error": "Upload failed"}
                throw result.error;
            }
            return result;
        }
      }
    });