jQuery文件上传与IE10,零字节文件卡在挂起状态 [英] jQuery file upload stuck in pending state with IE10, zero byte file

查看:877
本文介绍了jQuery文件上传与IE10,零字节文件卡在挂起状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



http://blueimp.github.io/jQuery-File-Upload/

即使演示中显示上传服务器当前不可用,您仍然可以通过转到演示并尝试上传零字节文件(必须有JPG,GIF或PNG扩展名,因为这是他们允许的)来重新创建问题。 / p>

在FireFox和Chrome中,您会看到它尝试POST零字节文件。在IE10中,如果您在开发人员工具中查看网络选项卡,则会看到一个XMLHTTPRequest对象处于挂起状态。如果你在IE10中上传一个非零大小的文件,你也会看到POST数据。



我搜索了一下,发现只有其他一些人抱怨IE10中的零字节文件。他们没有使用和我一样的jQuery插件,所以我认为这是一个普遍的IE10问题。另外,迄今为止唯一的答案是不允许用户上传零字节文件。我想支持零字节文件,如果可能的话,它们在我的应用程序中有一些用法,我不时自己使用它们,所以我是谁叫我的用户疯狂想要这个功能。

我的选择是什么?

解决方案

仍希望有更好的解决方案,但缺乏这一点,这将是我的答案:

  $(elem).fileupload({
add:function (e,data){
var file = data.files [0];
if(file.size == 0&& ie> = 10){
ajaxPostFileNameOnly .name);
}
else {
data.submit();
}
}
});

ie是我应用程序中的一个全局变量,设置为IE版本,如果不是IE,则为0。我知道功能检测通常更好,但在这种情况下,不知道如何检测这个奇怪的问题,因为一些浏览器总是报告零作为文件大小(Safari我认为),我也不能总是相信。 >

ajaxPostFileNameOnly只是一个简单的AJAX请求,只发送文件名,而不是执行库的普通文件上传。



编辑︰我结束了现在只是这样做,这使得IE10工作像IE的这个文件上传库的以前版本,只是使用iFrame的运输。

  $(elem).fileupload({
forceIframeTransport:!! ie
});

以及一些检测IE版本的代码我把两个不同的SO答案放在一起(警告,不会检测IE11 +,最有可能的报告为IE10):

  var ie =(function(){

var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');

while(
div.innerHTML ='<! - [if gt IE'+(++ v)+']>< i><![endif] - > ;',
all [0]
);

/ * @ cc_on
if(/^10/.test(@_jscript_version)){
v = 10;
}
@ * /

return v> 4?v:undef;

}());


I'm using this jQuery plug-in and seeing the same results on my website as I can reproduce on this demo:

http://blueimp.github.io/jQuery-File-Upload/

Even though the demo says "Upload server currently unavailable" you can still recreate the issue by going to the demo and trying to upload a zero byte file (must have a JPG, GIF or PNG extension since that is all they allow).

In FireFox and Chrome you will see it attempts to POST the zero byte file. In IE10, if you view the network tab in the developer tools, you will see an XMLHTTPRequest object stuck in "pending" state. If you upload a file of non-zero size in IE10, you will also see it POST the data.

I searched and found only a couple other people complaining about issues with zero byte files in IE10. They are not using the same jQuery plug-in as me, so I think it is a general IE10 issue. Also, the only answer I have found so far is to not allow users to upload zero byte files. I would like to support zero byte files if possible, they have some usage in my application and I use them myself from time to time, so who am I to call my users crazy for wanting this feature.

What are my options?

解决方案

Still hoping for a better solution, but lacking that, this will be my answer:

$(elem).fileupload({
    add: function(e, data) {
        var file = data.files[0];
        if (file.size == 0 && ie >= 10) {
            ajaxPostFileNameOnly(file.name);
        }
        else {
            data.submit();
        }
    }
});

ie is a global variable in my app, set to the IE version, or 0 if not IE. I know feature detection is usually better, but in this case not sure how to detect this weird issue and because some browsers always report zero as the file size (Safari I think), I also can't just always trust that.

The ajaxPostFileNameOnly would be a function that just makes a simple AJAX request to send only the file name, instead of doing the normal file upload that the library does.

EDIT: I ended up just doing this for now, which makes IE10 work like prior versions of IE for this file upload library and just use an iFrame for the transport.

$(elem).fileupload({
    forceIframeTransport: !!ie
});

and some code to detect IE versions I put together from a couple different SO answers (warning, will not detect IE11+, will report those as IE10 most likely):

var ie = (function() {

var undef,
    v = 3,
    div = document.createElement('div'),
    all = div.getElementsByTagName('i');

while (
    div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
    all[0]
);

/*@cc_on
if (/^10/.test(@_jscript_version)) {
    v = 10;
}
@*/

return v > 4 ? v : undef;

} ());

这篇关于jQuery文件上传与IE10,零字节文件卡在挂起状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆