Javascript 将错误消息传递给 jQuery 文件上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8097522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing the error message to jQuery File Upload
提问by stivlo
I've integrated JQuery File Uploadinto a Java Spring application. The returned JSON is generated from an array of Picture, where Picture contains "name", "size", "url", "thumbnail_url", "delete_url" and "delete_type".
我已将JQuery 文件上传集成到 Java Spring 应用程序中。返回的 JSON 由图片数组生成,其中图片包含“名称”、“大小”、“url”、“thumbnail_url”、“delete_url”和“delete_type”。
Sometimes I decide to not accept the upload on the server, to reject it, because of missing pre-conditions, so I would like to inform inform the user about it, by returning to the client an error message.
有时我决定不接受服务器上的上传,拒绝它,因为缺少前提条件,所以我想通过向客户端返回错误消息来通知用户。
I know it's possible to return an error message and an error code to the File Upload plugin, but I can't find it in the documentation. I suppose that I've to add two fields similar to "error_message" and "error_code".
我知道可以向文件上传插件返回错误消息和错误代码,但我在文档中找不到它。我想我必须添加两个类似于“error_message”和“error_code”的字段。
Where can I find this documentation or what are the field names that I should return.
我在哪里可以找到这个文档或者我应该返回哪些字段名称。
采纳答案by stivlo
By looking at the source code(the ultimate documentation), I found out that File Upload checks an "error" field and displays that error. I tried and it works, my error gets displayed.
通过查看源代码(最终文档),我发现文件上传检查“错误”字段并显示该错误。我试过了,它有效,我的错误被显示出来。
The JSON response is an array of Objects, one per file uploaded. In case of error, don't fill URLs and size. The important properties are the error
and name
and size
, that will be displayed to the user.
JSON 响应是一组对象,每个上传的文件一个。如果出现错误,请不要填写 URL 和大小。最重要的属性是error
和name
和size
,将被显示给用户。
[
{
"error": "Image must be in JPG format",
"url": "",
"thumbnail_url": "",
"delete_url": "",
"delete_type": "DELETE",
"name": "broken_image.jpg",
"size": 78191
}
]
回答by Lazaro Fernandes Lima Suleiman
You can use data.jqXHR.responseText
您可以使用 data.jqXHR.responseText
Like this:
像这样:
fail: function(e, data){
console.log(data.jqXHR.responseText);
},
回答by Allfarid Morales García
done: function (e, data){
var files = data.jqXHR.responseJSON.files;
console.log(files);
}),
Each "files" object can have an error var you can work with, like user stivlo said.
每个“文件”对象都可以有一个您可以使用的错误变量,就像用户 stivlo 所说的那样。