【问题标题】:ie9 javascript download function doesn´t workie9 javascript下载功能不起作用
【发布时间】:2017-01-23 15:52:29
【问题描述】:

我用JS为IE9创建了新的下载功能,但它不起作用。

descargarArchivo : function (url, idlote) {
            var str = window.location.href+"/"+url;
            str = str.replace("form.do/", "");
            // Le da nombre del fichero que se va a descargar
            var filename = 'factura_'+idlote;
            xhr= new XMLHttpRequest(); 
            xhr.responseType = 'blob';
            xhr.onload = function() {
                      var a = document.createElement('a');
                      a.href = window.URL.createObjectURL(xhr.response); 
                      a.download = filename; // Set the file name.
                      a.style.display = 'none';
                      document.body.appendChild(a);                         
                      a.click();
                      delete a;
                  }
               } 
           };
           xhr.open('GET', str);
           xhr.send();
        }

我读到,在 IE9 中没有 Blob 类型,所以xhr.response 返回未定义。我该如何解决?

【问题讨论】:

  • 您是否在 stackoverflow 上搜索过解决方案,似乎已经有人问过这个问题? IE9 Blob Polyfill
  • 是的,但是我有很多问题没有答案。
  • 你不能使用 Ajax 代替提交表单并让服务器设置下载标题。
  • 你应该为你的问题添加更多的精确度。您的文件始终是 PDF 格式吗?
  • 没有。我的文件是 html 或 xml。

标签: javascript internet-explorer internet-explorer-9


【解决方案1】:

看看这个答案:https://stackoverflow.com/a/1926163/2435443

他使用注入的VBScript (ActiveXObject) 将字节字符串转换为二进制数组,这是一种没有对象定义的Blob“模拟”。看起来又快又健壮。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2014-04-18
    相关资源
    最近更新 更多