Javascript execcommand("SaveAs",null,"file.csv") 在 IE8 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2515791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
execcommand("SaveAs",null,"file.csv") is not working in IE8
提问by Anbu
var doc = w.document;
doc.open('application/CSV','replace');
doc.charset = "utf-8";
doc.write("all,hello");
doc.close();
if(doc.execCommand("SaveAs",null,"file.csv")) {
window.alert("saved ");
}else {
window.alert("cannot be saved");
}
not working in IE 8
不适用于 IE 8
but woks in IE 6
但在 IE 6 中工作
what is the problem ? it is alerting "cannot be saved"
问题是什么 ?它正在警告“无法保存”
help me !!! advance thanks
帮我 !!!提前谢谢
回答by Andy E
The problem seems to be caused by an old bug that was fixed in Windows XP but is apparently unpatched in my Windows 7. From http://support.microsoft.com/kb/929863:
该问题似乎是由 Windows XP 中修复的旧错误引起的,但在我的 Windows 7 中显然未修补。来自http://support.microsoft.com/kb/929863:
This problem occurs because of a limitation in the ExecCommandfunction. When you run the script that uses the ExecCommandfunction together with the SaveAscommand, the script can only save a file that is the text file type.
由于ExecCommand函数中的限制,会出现此问题。当您运行同时使用ExecCommand函数和SaveAs命令的脚本时,该脚本只能保存文本文件类型的文件。
Sure enough, change the file extension to ".txt" and watch it magically work in IE8.
果然,将文件扩展名更改为“.txt”,然后在 IE8 中观看它神奇地工作。
The only workaround that comes to mind is to have a server-side language create the CSV file, and serve it up as a download (using the Content-disposition: attachmentheader).
想到的唯一解决方法是让服务器端语言创建 CSV 文件,并将其作为下载(使用Content-disposition: attachment标题)提供。
回答by Supagusti
I know this is an very old thread, but I also faced this problem. You can fix it by adding the following value (depending on the extension of the file beeing downloaded) to your registry.
我知道这是一个非常古老的线程,但我也遇到了这个问题。您可以通过将以下值(取决于正在下载的文件的扩展名)添加到您的注册表来修复它。
E.g. for .csv file:
例如对于 .csv 文件:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.csv]
"PerceivedType"="document"
@MICROSOFT: Shame on you; problem seems to be still unpatched :-)
@MICROSOFT:为你感到羞耻;问题似乎仍未解决:-)
回答by Matt Blaine
If you could do this processing server side with PHP or .NET or Java or something, in the http headers set:
如果您可以使用 PHP 或 .NET 或 Java 或其他东西来处理服务器端,请在 http 标头集中:
Content-Disposition: attachment; filename=file.csv
Which, unfortunately you can't do from JavaScript.
不幸的是,您不能从 JavaScript 中做到这一点。

