【发布时间】:2016-04-16 22:14:40
【问题描述】:
我正在尝试在 iFrame 中下载并显示远程文件的内容,并且在除 IE 之外的所有浏览器中都成功(我正在尝试使用 IE 10)。 我已经使用 XMLHttpRequest、Blob、CreateOBjectUrl API 来完成这个过程。
在 IE 中,我无法查看 iFrame 中的文件内容,并且控制台上也没有出现特定的错误消息。
我把我的代码贴在了这个帖子的底部,下面是一步一步的解释
- 获取下载文档的url和对应的mime 类型(在所有浏览器中都很好)。
- 调用 XMLHttp 请求,一个 Http GET 异步调用,响应类型为“arraybuffer”(完美 在所有浏览器中都很好)完成 XMLHttpGet 后,以下 3 个步骤是 执行。
- 使用正确的 mimetype 创建 blob ;(在所有其他浏览器中都很好,特别是通过使用 MSSaveOrOpenBlob 方法在 IE 中下载 blob 来验证 blob)。 4.为了将blob内容绑定到iFrame,使用“createObjectURL”创建blob url(在所有浏览器中都很好,但在IE中我们没有得到完美的URL)。
- 最后将 URL 与 iFrame 绑定显示。
代码如下:sn-p。
// Getting the document url and mime type ( Which is perfectly fine )
var downloadUrl=finalServerURL + "DocumentService.svc/GetItemBinary?id=" + itemId + "&version=" + version;
var mimeTypeForDownload = responseStore.mimeTypes[currentlySelectedObject.fileExtension];
window.URL = window.URL || window.webkitURL;
//Defining the XML Http Process
var xhr = new XMLHttpRequest();
xhr.open('GET', downloadUrl, true);
xhr.responseType = 'arraybuffer'; //Reading as array buffer .
xhr.onload = function (e) {
var mimeType = mimeTypeForDownload;
var blob = new Blob([xhr.response], { type: mimeType });
// Perfect blob, we are able to download it in both IE and non-IE browsers
//This below url from createObjectURL,
//Working perfectly fine in all non-IE browsers, but nothing happening in IE
var url = window.URL.createObjectURL(blob);
document.getElementById(documentContentiFrameId).setAttribute("src", url);
};
xhr.send;
如果您有任何相关信息,请告诉我,这将非常有帮助。
【问题讨论】:
标签: javascript html iframe internet-explorer-10