简体   繁体   English

JavaScript:通过IE中的数据URL保存文件

[英]JavaScript: Save file via data URL in IE

I have an app that converts files. 我有一个可以转换文件的应用程序。 I'm sending a file, and the converted file is being returned in the form of a data URL. 我正在发送文件,转换后的文件以数据URL的形式返回。 Had everything working great in Chrome, but IE (10/11/Edge) is a different story. 在Chrome上一切正常运行,但IE(10/11 / Edge)则完全不同。 Have tried several things to no prevail: 尝试了几件事以至不占优势:

1) HTML5 download attribute is not supported in IE. 1)IE不支持HTML5下载属性。 I tried assigning the returned URL as an anchor tag href, but invoking .click() is not supported, and manually clicking on the link does nothing. 我尝试将返回的URL分配为锚标记href,但是不支持调用.click() ,并且手动单击链接不会执行任何操作。

2) window.navigator.msSaveOrOpenBlob() and File Saver.js . 2)window.navigator.msSaveOrOpenBlob()和File Saver.js The SaveAs dialog pops up, but the blob is empty and never downloads anything. 会弹出“另存为”对话框,但是blob为空,从不下载任何内容。

var file= new Blob([returnedFile], {type: "application/pdf"});
window.navigator.msSaveOrOpenBlob(file, 'doc.pdf');
FileSaver.saveAs(file, 'doc.pdf');

Any ideas or suggestions to try? 有什么想法或建议可以尝试吗?

First, try to verify saveAs existance: 首先,尝试验证saveAs存在:

if (window.saveAs) { 
    window.saveAs(blob, name); 
} else { 
    navigator.saveBlob(blob, name); 
}

As for the Blob itself: 至于Blob本身:

  • create <a> 创建<a>
  • update href : 更新href

    a.href = window.URL.createObjectURL(new Blob(returnedFile, {type: "application/pdf"})); a.href = window.URL.createObjectURL(new Blob(returnedFile,{type:“ application / pdf”})));

  • fire click event click事件

More or less the same functionality can be reviewed there: http://jsfiddle.net/VB59f/2/ 可以在此处查看或多或少的相同功能: http : //jsfiddle.net/VB59f/2/

Ended up getting the browser via navigator.userAgent.match and handling the save based on each browser accordingly: 最终通过navigator.userAgent.match获取浏览器,并相应地基于每个浏览器处理保存:

 var saveData = (data, fileName) => {

IE: IE:

FileSaver.saveAs(blob, fileName + "." + extension);

Chrome: 铬:

var downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
downloadLink.style.display = "none";
downloadLink.href = data;
downloadLink.download = fileName;
downloadLink.click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM