【问题标题】:IE + XMLHttp + CreateObjectURL ErrorIE + XMLHttp + CreateObjectURL 错误
【发布时间】:2016-04-16 22:14:40
【问题描述】:

我正在尝试在 iFrame 中下载并显示远程文件的内容,并且在除 IE 之外的所有浏览器中都成功(我正在尝试使用 IE 10)。 我已经使用 XMLHttpRequest、Blob、CreateOBjectUrl API 来完成这个过程。

在 IE 中,我无法查看 iFrame 中的文件内容,并且控制台上也没有出现特定的错误消息。

我把我的代码贴在了这个帖子的底部,下面是一步一步的解释

  1. 获取下载文档的url和对应的mime 类型(在所有浏览器中都很好)。
  2. 调用 XMLHttp 请求,一个 Http GET 异步调用,响应类型为“arraybuffer”(完美 在所有浏览器中都很好)完成 XMLHttpGet 后,以下 3 个步骤是 执行。
  3. 使用正确的 mimetype 创建 blob ;(在所有其他浏览器中都很好,特别是通过使用 MSSaveOrOpenBlob 方法在 IE 中下载 blob 来验证 blob)。 4.为了将blob内容绑定到iFrame,使用“createObjectURL”创建blob url(在所有浏览器中都很好,但在IE中我们没有得到完美的URL)。
  4. 最后将 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


    【解决方案1】:

    我知道在 IE 中无法为您的 blob 条目获取正确的 URL,我的尝试都没有成功。 我的替代解决方案, 1) 使用 pdf.js,一个开源的 javascript 库,它允许渲染 pdf 二进制文件和等效的 pdf blob。 2)利用开放的PDF库编写自己的查看器,这将是耗时的,并且涉及更多的学习工作。

    谢谢, 毗湿奴

    【讨论】:

      猜你喜欢
      • 2022-10-07
      • 1970-01-01
      • 2012-01-30
      • 2021-12-13
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多