【问题标题】:File Upload fails, but jqXHR returning status 200 in IE8文件上传失败,但 jqXHR 在 IE8 中返回状态 200
【发布时间】: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


【解决方案1】:

对不起我上面的评论,不在服务器端。我仔细阅读了这个页面:http://www.checkupdown.com/status/E400.html

我在这里放了一句话,让我大开眼界,也许你也大开眼界:)

Do you get the error using more than one browser ?. If you have two or more Web browsers installed on your PC and the behaviour is not the same (one Web browser gives an HTTP 400 error visiting a site, another Web browser does not give the 400 error visiting the same site), then one of your browsers may be defective. Try to find an upgrade or security fix for the problem browser. If you recently changed some configuration options in the problem browser, try reversing the change to see if that helps.

请同时阅读该页面的其他要点。

【讨论】:

  • 嗨 SnakeEyes,感谢您的链接。我的问题可能不是很清楚。我实际上希望看到 400 响应,但我在 IE8 中得到了 200 响应。我想看到错误响应,但浏览器以某种方式将其解释为成功响应。遗憾的是,升级浏览器不是一种选择,因为这是为不想从 IE8 升级的企业客户准备的。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 2017-07-12
  • 2021-03-01
相关资源
最近更新 更多