【问题标题】:PDF file name not taken into account in IE10 / EdgeIE10/Edge 中不考虑 PDF 文件名
【发布时间】:2016-03-28 23:25:30
【问题描述】:

使用此代码,我的 pdf 名称不正确:一串随机字母,如 uuid。 这个问题似乎只出现在 IE 10 / Edge 上。 AngularJS 的 1.4.7 版本

this.downloadPdf = function(pdfName){
    console.log(pdfName);
    $http.get(config.UrlApi + "/pdf/"+ pdfName.nameFile, { responseType: 'arraybuffer' });
     var a = document.createElement('a');
     a.href = URL.createObjectURL(blob);
     a.target = '_blank';
     a.download = pdfName.name;
     document.body.appendChild(a);
     a.click();
 });

【问题讨论】:

    标签: angularjs pdf cross-browser internet-explorer-10 microsoft-edge


    【解决方案1】:

    使用 Blob 接口:

    function build(response)
      {
      var bb = new Blob([response.data], { type: "application/pdf"});
    
      if (URL && URL.hasOwnProperty("createObjectURL") )
        {
        var url = URL.createObjectURL(bb);
        }
      else if (window.navigator.msSaveOrOpenBlob)
        {
        window.navigator.msSaveOrOpenBlob(bb, 'foo.pdf');
        }
      }
    
    $http.get(config.UrlApi + "/pdf/"+ pdfName.nameFile, { responseType: 'arraybuffer' }).then(build);
    

    Blob url 受制于原始策略。这意味着它们只能用于与运行创建 url 的脚本的文档具有相同来源的文档中。如果您需要使用在不同域中运行的 <iframe> 中的 blob 对象,则必须使用 postMessage API 将 blob 数据发送到框架,然后在那里创建 blob: url。

    参考文献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-03
      • 2015-08-14
      • 1970-01-01
      • 2013-10-09
      • 2016-03-29
      • 1970-01-01
      • 2019-02-25
      • 2018-02-28
      相关资源
      最近更新 更多