我正在使用https://github.com/blueimp/jQuery-File-Upload
它在现代浏览器中使用XHR文件上传,在旧版中使用隐藏的iframe上载(8,9)。
当文件验证失败时,服务器返回4xx状态代码(错误/失败)。
$('#fileupload').fileupload({
url: url,
dataType: 'json',
done: function (e, data) {
console.log("done")
},
fail: function (e, data) {
console.log("fail")
}
})然而,在旧式中,即使我发送4xx状态代码,也会调用已完成的回调。有没有办法让它调用失败回调(https://github.com/blueimp/jQuery-File-Upload/wiki/Options#fail)并读取响应体或状态代码?
我认为它可能需要使用隐藏的iframe方法来上传文件,但是我在文档中找不到任何具体的东西。
发布于 2014-01-17 16:58:37
我应该检查一下常见问题
// 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;
}
}
});https://stackoverflow.com/questions/21176964
复制相似问题