JS Base64 string to downloadable pdf - Edge

2019-07-07 16:52发布

问题:

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