jquery文件上传 - IE做回调data.result问题 [英] jquery file upload - IE done callback data.result issue

查看:293
本文介绍了jquery文件上传 - IE做回调data.result问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery 文件上传插件

我不使用 UI 部分,只有基本的一部分。



当我执行以下操作时,出现 IE 的问题:

<$ p ($ data $)$ $ $ $ $''$ file $'$'$'$'$'$'$'$' (data.result!= null&& $ .trim(data.result)!='')
$('a#attachment')。html(data.result);
}
// ......................

IE中 data.result Object IE9 至少,不知道其他人是什么...)



Chrome / Firefox 只是响应 Text )。

创意?

解决方案

问题,我想我t是由于XHR文件上传在IE中不支持(甚至是9)。这是我相信正在发生的事情和我的解决方案:

由于Safari 5+,Firefox 4+和Chrome支持XHR文件上传,因此fileupload插件可以真正地将文件异步,允许来自服务器的纯文本响应。这个纯文本响应可以通过 done 事件处理程序中的 data.result 获得,并且可以很容易地使用。在IE中,文件传输是通过在隐藏的iframe中进行全页面刷新,导致 data.result in done 处理程序是一个完整的文档对象,响应文本深深地包含在< ; html>< body>< iframe>< pre>< / code>标记 这不仅会让人感到痛苦在IE中的数据,它使得你得到不同的浏览器之间的数据。



我的解决方案:

我将 forceIframeTransport 选项设置为true,这使得所有浏览器都使用隐藏的iframe来传输文件,如IE。不幸的是错过了XHR文件上传,但是这至少给了我们在所有浏览器中的同样的响应。然后,在我的 done 处理程序中,我从文档对象中提取结果,如下所示:

  var result = $('pre',data.result).text(); 

在您的情况下,我认为代码看起来像这样:

  $('input#fileupload')。fileupload({
forceIframeTransport:true,
url:'/ upload',
done:function(e,data){
var result = $('pre',data.result).text();
if(result!= null&& $ .trim (result)!='')
$('a#attachment').html(result);
}
...

这里还需要注意的是,来自我的服务器的响应的内容类型是'text / plain',正如您可能已经注意到的,IE有时会提示用户保存或打开一个JSON响应。

以下是解决问题时有用的几个链接:
https://github.com/blueimp/jQuery-File-Upload/wiki/Browser-support (请参阅'XMLHttpRequest'部分在底部)
https://github.com/blueimp/jQuery-File-Upload/wiki/Options (参见'forceIframeTransport'about三分之一的下降)



希望这有助于!


I'm using jQuery file upload plugin.

I don't use the UI part, only basic one.

When I do the following, I'm having an issue in IE:

$('input#fileupload').fileupload({
    url: '/upload',
    done: function (e, data) {
    if(data.result != null && $.trim(data.result) != '')
        $('a#attachment').html(data.result);
    }
    //......................

on IE the data.result is an Object (IE9 at least, not sure the others...)

On Chrome/Firefox is just the response Text (simple plain text from the server).

Ideas?

解决方案

I just ran into the same problem, and I think it is due to the fact that XHR file uploads are not supported in IE (even 9). Here's what I believe is happening, and my solution:

Since Safari 5+, Firefox 4+, and Chrome support XHR file uploads, the fileupload plugin can transfer the files truly asynchronously, allowing for a pure text response from the server. This pure text response is available via data.result in the done event handler, and can be used easily.

In IE, however, the file transfer occurs via a full page refresh in a hidden iframe, causing data.result in the done handler to be a full document object, with the response text wrapped deep inside <html><body><iframe><pre> tags.

Not only does this make it a pain to get to the data in IE, it makes the way you get to the data different between browsers.

My solution:

I set the forceIframeTransport option to true, which makes all browsers transfer files with a hidden iframe, like IE. It's unfortunate to miss out on XHR file uploads, but this at least gives us the same kind of response in all browsers. Then, in my done handler, I extracted the result from the document object like so:

var result = $( 'pre', data.result ).text();

In your case, I think the code would look something like this:

$('input#fileupload').fileupload({
    forceIframeTransport: true,
    url: '/upload',
    done: function ( e, data ) {
     var result = $( 'pre', data.result ).text();
     if( result != null && $.trim( result ) != '' )
        $( 'a#attachment' ).html( result );
    }
...

Also important to note here is that the Content Type of the response from my server is 'text/plain'. As you may have noticed, IE sometimes prompts the user to save or open a json response.

Here are a couple links that proved useful when resolving the problem: https://github.com/blueimp/jQuery-File-Upload/wiki/Browser-support (see 'XMLHttpRequest' section at bottom) https://github.com/blueimp/jQuery-File-Upload/wiki/Options (see 'forceIframeTransport' about 1/3 of the way down)

Hopefully this helps!

这篇关于jquery文件上传 - IE做回调data.result问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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