使用FileSaver在IE11上保存文件

使用FileSaver在IE11上保存文件

问题描述:

我正在使用FileSaver库( https://github.com/eligrey/FileSaver.js ),并且在IE11上不起作用,对于其他浏览器,我没有问题.

I'm using FileSaver library ( https://github.com/eligrey/FileSaver.js) and does not work on IE11, with other browsers I had no problem.

代码是这样的:

var file = new File(["content"], "sample.xml", { type: "application/xml;charset=utf-8" });
saveAs(file);

执行第一条指令(新)时出现此错误:

I'm getting this error when the first instruction (new) executes:

对象不接受此操作"

"the object does not accept this action"

git hub上有一个未解决的问题,但实际上没有解决方案,我正在寻找一种应在IE11上正常工作的解决方法,如下所示:

There's an open issue on git hub, but actually with no solution, I'm looking for a workaround that should work on IE11, like this:

try {
                var file = new File([msg.d], "test.xml", { type: "application/xml;charset=utf-8" });
                saveAs(file);
     } catch (err) {
                // Code that works on IE11 ....
     }

任何帮助都应得到赞赏.

Any help should be appreciated.

http://caniuse.com/#搜索=文件 [2]某些浏览器不支持File构造函数.

http://caniuse.com/#search=file [2] Some browser don't support the File constructor.

获取文件实例的唯一方法是通过input[type=file]

The only way you can get a File instance is through input[type=file]

与其将它包装在try/catch中,还不如不这样做:

instead of wrapping it around a try/catch why not just do this:

var blob = new Blob(['content'], { type: 'application/xml' });
saveAs(blob, fileName);