【问题标题】:JQuery File Upload plugin, Internet Explorer, and IFrame transportJQuery 文件上传插件、Internet Explorer 和 IFrame 传输
【发布时间】:2012-11-08 18:14:47
【问题描述】:

我正在使用this plugin(只是基本版本),并且正在利用所有简洁的功能进行进度更新,以使用我自己的 UI(使用 Knockout.js 和 Twitter Bootstrap)。下面是一些用于上下文的代码 sn-ps:

    // The file is sent to an ASP.NET MVC Web Api service to do all the business logic/DB stuff
    uploadUrl = http://web.api.url/?apikey=key

    $("#fileUpload" + "@index").fileupload({
        headers: {
            'Authorization': "@Html.AccessToken()",
            'Accept': $.support.ajax ? "application/json" : "text/plain"
        },
        url: uploadUrl,
        add: function (e, data) {
            $.each(data.files, function (index, file) {
                // add to KO viewmodel
            });
            data.submit();
        },
        fail: function (e, data) {
            var error = data.errorThrown;
            var text = data.textStatus;
        },
        done: function (e, data) {
            // do some more viewmodel operations
        },
        progress: function (e, data) {
            var progressPercentage = parseInt(data.loaded / data.total * 100, 10);
            // update viewmodel
        }
    });

#fileUpload<Index> 元素是文件输入

这在 Chrome、FF 和 Safari 中效果很好,但在 IE 中却不适用。尝试从我的文件输入中选择一个文件时,我得到一个非常奇怪的响应 - 浏览器打开一个下载对话框?!

Do you want to open or save ?apikey=key (61 bytes) from webapiserver?

我尝试在我的文件上传事件侦听器中使用带有断点的 IE 脚本调试器,但它甚至没有进入其中。我在我的研究中看到了各种帖子和文章,表明接受类型的 application/json 会搞砸 IE,所以我的代码中有一个条件来尝试处理它。

我有什么遗漏的吗?

【问题讨论】:

  • 你有没有解决这个问题?

标签: jquery iframe file-upload knockout.js internet-explorer-9


【解决方案1】:

您的问题出在服务器端。

IE有点笨,他接受服务器提供的第一个内容类型,所以你总是需要记住首先尝试用text/plain或text/html回答。

不过有人提到了in the FAQ from the jQuery plugin。

【讨论】:

    【解决方案2】:

    这是一个对我有用的 C# 修复实现

        var result = Json(statuses, JsonRequestBehavior.AllowGet);
    
        if (HttpContext.Request.AcceptTypes.Contains("application/json")) 
        {
            result.ContentType = "application/json";
        } 
        else // Hack for IE9 
        {
            result.ContentType = "text/plain";
        }
    
        return result;
    

    【讨论】:

      【解决方案3】:

      这是因为返回的数据是application/json。您需要服务器指定 content-type text/html 以防止 IE 将其解释为下载。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-15
        • 2011-07-19
        • 1970-01-01
        • 2012-09-28
        • 2011-12-16
        • 1970-01-01
        • 2010-10-09
        相关资源
        最近更新 更多