【发布时间】:2013-06-20 12:24:06
【问题描述】:
我正在使用jQuery File Upload 插件来调用网络服务并将图像上传到服务器。
图片上传正常,我已经向网络服务添加了一些验证,如果图片大于 4MB,则会引发异常。
这是我使用文件上传插件的方式:
var fileUploadInput = $('#uploader').fileupload({
dataType: 'json',
add: function (e, data) {
mainObject._addFile(e, data, mainObject);
},
done: function (e, data) {
mainObject._fileUploadDone(e, data, mainObject);
},
fail: function (e, data) {
mainObject._fileUploadFail(e, data, mainObject)
}
});
现在,当文件上传失败时,参数data包含一个jqXHR对象如下:
readyState: 4
status: 200
statusText: "success"
在 Chrome 中,jqXHR 包含由 Web 服务引发的异常生成的可爱错误消息。在 Fiddler 中,我可以看到响应是 400 Bad Response,我可以在响应正文中看到错误消息。
有谁知道为什么这在 IE8 中没有得到支持?
更新
我们的服务器端代码的简化版本:
[OperationContract(), WebInvoke(UriTemplate = "/Images", Method = "POST")]
public System.Guid CreateImage(System.IO.Stream Image)
{
//IE8 needs response to be in plain text format, or it will attempt to download the response :(
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Type", "text/plain");
//perform validation
BLL.Errors validationErrors = BLL.Utilities.ImageFileProcessing.ValidateSizeAndFormat(multipartRequestParser.FileContents);
//if there were errors
if (validationErrors.Count > 0)
{
//send back the errors
throw new WebFaultException<BLL.Errors>(validationErrors, System.Net.HttpStatusCode.BadRequest);
}
//save the image (returns the identifier for the image)
System.Guid imageId = BLL.Custom.BeamUSBuyingWindow.DataTransferObjects.Brand.SaveImage(multipartRequestParser.FileContents);
//set the response status and URI for created resource
WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(new System.Uri(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri.AbsoluteUri + "/" + imageId.ToString()));
return imageId;
}
ValidateSizeAndFormat 方法返回一个 Errors 对象数组,其中包含我期望的错误文本。这是一个相当大的应用程序。
Chrome 在 jqXHR 对象中检测到 400 错误,而 IE8 检测到 200 成功时,我感到有些困惑。尽管其他地方有类似的 Ajax 错误处理,但这个问题只发生在 File Upload 插件中。
【问题讨论】:
-
400 你从服务器端获得...尝试发布在 web service/asp.net 中使用的代码
-
我添加了一些服务器端代码。这是一个相当大的应用程序:D
-
@SimonAdcock 我在使用 v9.9.2 时遇到了同样的问题 - 你有没有找到解决办法?
-
@shrodes 恐怕不行。我必须按照
if (data.jqXHR.responseText) { /* assume failure */ }的方式实施一个hack,这在我的情况下有效,但不是一个很好的通用解决方案。 -
我的解决方案是检查
data.result。如果是undefined,则有错误。但我仍然不知道为什么blueimp-file-upload总是将状态码检测为 200,即使存在服务器错误或网络错误。不过它在 Chrome 上运行良好。
标签: jquery asp.net json web-services jquery-file-upload