JS Base64 字符串到可下载的 pdf - Edge

Jea*_*and 2 html javascript pdf base64 microsoft-edge

我现在有一个问题,在所有浏览器和平台中,我可以使用以下方法将 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);
}
Run Code Online (Sandbox Code Playgroud)

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

在此先感谢您的帮助。

Lai*_*ain 5

如果我没记错的话,那是 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)
}
Run Code Online (Sandbox Code Playgroud)