JavaScript:通过IE中的数据URL保存文件 [英] JavaScript: Save file via data URL in IE

查看:503
本文介绍了JavaScript:通过IE中的数据URL保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个转换文件的应用。我正在发送文件,转换的文件以数据URL的形式返回。如果Chrome中的一切都运行良好,但IE(10/11 / Edge)是另一回事。尝试过几件事情并不占优势:

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. Had everything working great in Chrome, but IE (10/11/Edge) is a different story. Have tried several things to no prevail:

1)IE不支持HTML5下载属性。我尝试将返回的URL指定为锚标记href,但不支持调用 .click(),并且手动单击该链接不会执行任何操作。

1) HTML5 download attribute is not supported in IE. 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.

2)window.navigator.msSaveOrOpenBlob()和 File Saver.js 。 SaveAs对话框弹出,但blob为空,从不下载任何内容。

2) window.navigator.msSaveOrOpenBlob() and File Saver.js. The SaveAs dialog pops up, but the blob is empty and never downloads anything.

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?

推荐答案

通过 navigator.userAgent.match 结束浏览器因此,根据每个浏览器处理保存:

Ended up getting the browser via navigator.userAgent.match and handling the save based on each browser accordingly:

 var saveData = (data, fileName) => {

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

这篇关于JavaScript:通过IE中的数据URL保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆