JS Base64 string to downloadable pdf - Edge

故事扮演 提交于 2019-12-10 23:37:36

问题


I have an issue at them moment, where in all browsers and platforms, I can convert a bse64 string to a PDF file using:

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);
}

I have done some research, but I have not been able to find a solution where I can specify the file name, and have the file download automatically in Edge.

Thanks in advance for the help.


回答1:


If I recall correctly, that is a general problem in IE and not just Edge. One can save named blobs in IE by using msSaveOrOpenBlob():

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)
}


来源:https://stackoverflow.com/questions/40724578/js-base64-string-to-downloadable-pdf-edge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!