jQuery file upload plugin - fail callback not firi

2019-06-28 06:24发布

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.

1条回答
闹够了就滚
2楼-- · 2019-06-28 06:55

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;
    }
  }
});
查看更多
登录 后发表回答