【问题标题】:jQuery File Upload doesn't send a file in IE9jQuery File Upload 不会在 IE9 中发送文件
【发布时间】:2014-07-24 19:29:48
【问题描述】:

我只在 IE

$("input:file#upload-photo").fileupload({
    enctype: "multipart/form-data",
    url: $("input:file#upload-photo").attr("url"),
    autoUpload: true,
    send: function() {
        spinner.spin(target);
    },
    done: function (e, data) {
        spinner.spin(false);

        var errors = data.result.Errors;
        if (errors != null && errors.length > 0) {
            for (var i = 0; i < errors.length; i++) {
                var ul = $("<ul>").html("<li>" + errors[i] + "</li>");
                $('#upload_photo_errors').html(ul);
            }
        } else {
            $(".profile-photo").attr("src", data.result.Data.AvatarUrl);
        }
    },
    error: function() {
        spinner.spin(false);
    }
});

“发送”处理程序被调用,“错误”处理程序也被调用。

我在 Fiddler 中收到了我的请求。它没有 Content-Type 和任何数据:

POST http://172.20.40.45/site/api/Users/Avatar HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept: */*
Referer: http://172.20.40.45/site/Profile/Edit
Accept-Language: ru
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Host: 172.20.40.45
Content-Length: 0
DNT: 1
Connection: Keep-Alive
Pragma: no-cache
Cookie: ...

我已尝试检查http://blueimp.github.io/jQuery-File-Upload/ 上的上传过程。它在 IE9 中运行良好。为什么它在我的情况下不起作用?

【问题讨论】:

标签: javascript jquery internet-explorer file-upload blueimp


【解决方案1】:

我已经解决了这个问题。 起初我用 multipart/form-data 有效载荷解决了这个问题。 IE9 不支持 XHR 文件上传。因此我添加了下一个脚本:

<script src="~/Scripts/jquery.iframe-transport.js"></script>

我还专门为 IE9 添加了另一个脚本:

<!--[if lt IE 10]>
<script src="~/Scripts/jquery.xdr-transport.js"></script>
<![endif]-->

现在浏览器上传文件。但我遇到了另一个问题。这次我有这样的problem。我使用 Web API,发现 this solution 对我有帮助。

但现在调用的是“错误”处理程序而不是“完成”。为了解决这个问题,我们必须将 dataType 设置为 'json'。最终版本看起来是这样的:

$("input:file#upload-photo").fileupload({
    dataType: "json",
    url: $("input:file#upload-photo").attr("url"),
    autoUpload: true,
    send: function() {
        spinner.spin(target);
    },
    done: function (e, data) {
        var errors = data.result.Errors;
        if (errors && errors.length) {
            var items = $.map(errors, function (i, error) {
                return $("<li>").text(error);
            });
            $("<ul>").append(items).appendTo('#upload_photo_errors');
        } else {
            $(".profile-photo").attr("src", data.result.Data.AvatarUrl);
        }
    },
    always: function() {
        spinner.spin(false);
    }
});

【讨论】:

  • 感谢您发布您的解决方案。在星期天为我节省了几个小时:)
【解决方案2】:

我的问题是,如果通过JS触发文件输入,IE9不会发送文件,用户实际上需要点击浏览按钮才能工作。

【讨论】:

  • 我不知道,我不得不修改脚本,以便用户单击浏览按钮。
  • 是的,看起来通过 JS 触发它不是一个选项。但是,使用标签并设置正确的“for”属性可以满足我的需求。
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 2023-01-12
  • 1970-01-01
  • 2013-10-18
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
相关资源
最近更新 更多