IE9提示用户提交隐藏的iFrame [英] IE9 prompts user on submission of hidden iFrame

查看:109
本文介绍了IE9提示用户提交隐藏的iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试我们的文件上传脚本,该脚本在不支持通过XMLHttpRequest对象发送文件数据的浏览器中使用隐藏的iframe。

I am debugging our file uploading script, which uses a hidden iframe in browsers that do not support sending file data via an XMLHttpRequest object.

关于在IE中提交iframe,有大量文章和问题; (如此帖这篇文章),但他们都引用IE将无法正确设置' name'属性给你。

There is a plethora of articles and questions on submitting an iframe in IE; (like this post, and this post), but they all cite the fact that IE will not properly set the 'name' attribute for you.

下面的代码生成一个表单,其目标设置为iFrame的名称,但在提交表单时,IE9仍然提示我你想要打开还是保存 photo_upload.json ?,而不是将其加载到iFrame中。

The code below produces a form whose target is set to the name of the iFrame, but on submitting the form, IE9 still prompts me with "Do you want to open or save photo_upload.json?", instead of loading it into the iFrame.

var $iframe = $("<iframe id='" + id + "' name='" + id + "' />");
$('body').append($iframe);

$iframe.load(function () {
  console.log("loaded");  // this never happens in IE9
});

// pretend this form also has a file input object that gets populated
var $form = $('<form />').attr({
  method: "post",
  enctype: "multipart/form-data",
  action: "/photo_upload.json",
  target: id
});

$('body').append($form);
$form.submit();  

此代码适用于FF和Chrome,但在IE9中我会收到提示打开或保存' photo_upload.json 。如果我打开文件,IE9获得的响应是​​准确的。

This code works in FF and Chrome, but in IE9 I get prompted to open or save 'photo_upload.json'. The response that IE9 gets is accurate, if I open the file.

有没有人有任何见解?有什么我想让iframe提交通过吗?我是否需要设置某种标题来告诉IE不要这样打开它?谢谢!

Does anyone have any insight? Is there anything I'm missing to get the iframe submission to go through? Do I need to set a header of some sort to tell IE not to open it like that? Thank you!

为我解决的代码,感谢@Kolink。

Code that solved it for me, thanks to @Kolink.

Rails服务器端代码,在photo_upload函数中:

Rails server side code, in the photo_upload function:

if params[:from_iframe]
  render :json => @temp_photo, :content_type => "text/html"
else
  render :json => @temp_photo
end

我从iframe提交表单时传递一个额外的参数,所以我可以使用相同的服务器端功能,无论照片上传是AJAX请求还是隐藏的iFrame。

I pass an extra parameter when the form is submitted from the iframe, so I can use the same server side function whether the photo upload is an AJAX request, or a hidden iFrame.

在后一种情况下,正如@Kolink建议的那样,这将是从iFrame中获取JSON:

In the latter case, as @Kolink recommends, this will get the JSON out of the iFrame:

$iframe.load(function () {
  json_string = iframe[0].contentDocument.body.innerHTML;
});


推荐答案

photo_upload.json 正在下载到iframe中,但由于浏览器不知道如何处理它,它会认为它是一个可供下载的文件。

photo_upload.json IS being downloaded into the iframe, but because the browser doesn't know what to do with it, it assumes it to be a file for download.

理想情况下,您应该让iframe接收HTML数据,该数据可能包含< script> ,以告知父窗口要执行的操作。或者,如果您已经在父级中有代码来处理接收响应(看起来像你那样),请在强制响应上设置 Content-Type:text / html 浏览器将其视为HTML文档 - 它会自动添加基本信息,如< html> 标记和< body> 标记,所以只需访问 iframe.contentDocument.body.innerHTML

Ideally, you should have the iframe receive HTML data, which may contain a <script> to tell the parent window what to do. Alternatively, if you already have code in the parent to handle receiving the response (which it looks like you do), set Content-Type: text/html on the response to force the browser to treat it as an HTML document - it will automatically add the basics like an <html> tag and a <body> tag, so just access iframe.contentDocument.body.innerHTML.

这篇关于IE9提示用户提交隐藏的iFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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