【问题标题】:File upload using jquery works in Firefox but not in Internet Explorer使用 jquery 上传文件在 Firefox 中有效,但在 Internet Explorer 中无效
【发布时间】:2012-05-08 09:09:46
【问题描述】:

这个javascript代码

$(document).ready(function () {
            var uploader = new qq.FileUploader({
                element: document.getElementById('file-uploader'),
                action: 'api/GradeCheckIn',
                debug: true,
                onComplete: function (id, fileName, responseJSON) {
                    alert(responseJSON[0].ValidationErrors[0].ErrorMessage);
                }
            });
        });

在 Firefox 中就像一个魅力。文件被发送到服务器,结果返回给客户端。返回的数据是json btw。

但在 Internet Explorer 中,我收到消息 您要从 localhost 打开还是保存 GradeCheckIn(2 个字节)。 就像我正在尝试下载文件一样。

我该如何解决这个问题?我希望资源管理器中的行为与 Firefox 中的行为相同。

在 cmets 之后编辑:

IE 和 FireFox 请求头中的 content-type 不同:

IE: multipart/form-data; boundary=---------------------------7dc2ec8205b2

Firefox: application/json; charset=utf-8

即使我在发出请求的代码中明确设置了内容类型:

xhr.open("POST", queryString, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-File-Name", encodeURIComponent(name));
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.send(file);

我仍然无法解决这个问题。所有想法都受到高度赞赏。如果我应该提供更多信息,请告诉我。

【问题讨论】:

  • 听起来您从服务器发送的响应正在将无法识别的 mime 类型发送回隐藏的 iframe。它返回的是什么 mime 类型?
  • @Tejs 我在上面的问题中提供了有关内容类型的信息。

标签: javascript jquery asp.net-web-api file-upload


【解决方案1】:

大多数浏览器不将application/json 处理为返回内容类型。如果您仍想编写 json 并从 iframe 中读取它,您可能只考虑“破解”响应并发回您的内容,但使用 text/html 的 mime 类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2015-05-20
    • 2010-10-22
    • 1970-01-01
    相关资源
    最近更新 更多