IE + XMLHttp + CreateObjectURL 错误
IE + XMLHttp + CreateObjectURL Error
我正在尝试下载并显示 iFrame 中的远程文件的内容,并在除 IE 之外的所有浏览器中都成功了(我正在尝试使用 IE 10)。
我使用了 XMLHttpRequest、Blob、CreateOBjectUrl API 来完成这个过程。
在 IE 中,我无法查看 iFrame 中的文件内容,控制台上也没有出现任何特定的错误消息。
我把我的代码贴在了这个帖子的底部,下面是一步一步的解释
- 正在获取下载文件url & 对应的mime
类型(在所有浏览器中都很好)。
- 正在调用 XMLHttp 请求,一个
Http GET 异步调用,响应类型为 'arraybuffer'(完美
在所有浏览器中都很好)完成 XMLHttpGet 下面的 3 个步骤是
正在执行。
- 使用正确的 mimetype 创建 blob ;(在所有其他浏览器中都很好,通过使用 MSSaveOrOpenBlob 方法在 IE 中下载它来特别验证 blob)。
4.InOrder 将 blob 内容绑定到 iFrame,使用 "createObjectURL" 创建 blob url(在所有浏览器中都很好,但在 IE 中我们没有得到完美的 URL) .
- 最终将 URL 与 iFrame 绑定以进行显示。
下面的代码片段。
// 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;
如果你得到任何关于这方面的信息,请告诉我,这将非常有帮助。
我开始知道在 IE 中不可能为您的 blob 条目获得正确的 URL,我的 none 尝试都成功了。
我的替代解决方案,
1) 选择 pdf.js,一个开源 javascript 库,它允许呈现 pdf 二进制文件和等效的 pdf blob。
2)使用开放的PDF库编写自己的查看器,这将耗费时间,并且需要更多的学习努力。
谢谢,
毗湿奴
我正在尝试下载并显示 iFrame 中的远程文件的内容,并在除 IE 之外的所有浏览器中都成功了(我正在尝试使用 IE 10)。 我使用了 XMLHttpRequest、Blob、CreateOBjectUrl API 来完成这个过程。
在 IE 中,我无法查看 iFrame 中的文件内容,控制台上也没有出现任何特定的错误消息。
我把我的代码贴在了这个帖子的底部,下面是一步一步的解释
- 正在获取下载文件url & 对应的mime 类型(在所有浏览器中都很好)。
- 正在调用 XMLHttp 请求,一个 Http GET 异步调用,响应类型为 'arraybuffer'(完美 在所有浏览器中都很好)完成 XMLHttpGet 下面的 3 个步骤是 正在执行。
- 使用正确的 mimetype 创建 blob ;(在所有其他浏览器中都很好,通过使用 MSSaveOrOpenBlob 方法在 IE 中下载它来特别验证 blob)。 4.InOrder 将 blob 内容绑定到 iFrame,使用 "createObjectURL" 创建 blob url(在所有浏览器中都很好,但在 IE 中我们没有得到完美的 URL) .
- 最终将 URL 与 iFrame 绑定以进行显示。
下面的代码片段。
// 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;
如果你得到任何关于这方面的信息,请告诉我,这将非常有帮助。
我开始知道在 IE 中不可能为您的 blob 条目获得正确的 URL,我的 none 尝试都成功了。 我的替代解决方案, 1) 选择 pdf.js,一个开源 javascript 库,它允许呈现 pdf 二进制文件和等效的 pdf blob。 2)使用开放的PDF库编写自己的查看器,这将耗费时间,并且需要更多的学习努力。
谢谢, 毗湿奴