JS Base64 字符串到可下载的 pdf - Edge
JS Base64 string to downloadable pdf - 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);
}
我做了一些研究,但我一直无法找到可以指定文件名并在 Edge 中自动下载文件的解决方案。
在此先感谢您的帮助。
如果我没记错的话,这是 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)
}
我现在有一个问题,在所有浏览器和平台中,我可以使用以下方法将 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 中自动下载文件的解决方案。
在此先感谢您的帮助。
如果我没记错的话,这是 IE 中的普遍问题,而不仅仅是 Edge。可以使用 msSaveOrOpenBlob():
在 IE 中保存命名的 blobvar 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)
}