【问题标题】:JS Base64 string to downloadable pdf - EdgeJS Base64 字符串到可下载的 pdf - Edge
【发布时间】:2016-11-21 16:13:24
【问题描述】:

我现在有一个问题,在所有浏览器和平台中,我可以使用以下方法将 bse64 字符串转换为 PDF 文件:

function runReport_onComplete(response)
{
    var element = document.createElement('a');
    element.setAttribute('href', encodeURI("data:application/pdf;base64," + response.Report));
    element.setAttribute('download', "LoginInquiry.pdf");
    element.style.display = 'none';
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
}

我已经做了一些研究,但我找不到一个可以指定文件名并在 Edge 中自动下载文件的解决方案。

提前感谢您的帮助。

【问题讨论】:

  • window.top.navigator.msSaveOrOpenBlob(blob, 文件名)

标签: javascript html pdf base64 microsoft-edge


【解决方案1】:

如果我没记错的话,这是 IE 中的普遍问题,而不仅仅是 Edge。可以使用 msSaveOrOpenBlob() 在 IE 中保存命名的 blob:

var tF = 'Whatever.pdf';
var tB = new Blob(..);

if(window.top.navigator.msSaveOrOpenBlob){
    //Store Blob in IE
    window.top.navigator.msSaveOrOpenBlob(tB, tF)
}
else{
    //Store Blob in others
    var tA = document.body.appendChild(document.createElement('a'));
    tA.href = URL.createObjectURL(tB);
    tA.download = tF;
    tA.style.display = 'none';
    tA.click();
    tA.parentNode.removeChild(tA)
}

【讨论】:

    猜你喜欢
    • 2016-04-14
    • 2015-04-18
    • 1970-01-01
    • 2018-08-15
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多