简体   繁体   English

execcommand(“SaveAs”,null,“file.csv”)在IE8中不起作用

[英]execcommand(“SaveAs”,null,“file.csv”) is not working in IE8

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 提前谢谢

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 ExecCommand function. 出现此问题是由于ExecCommand函数的限制。 When you run the script that uses the ExecCommand function together with the SaveAs command, the script can only save a file that is the text file type. 当您运行使用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: attachment header). 想到的唯一解决方法是使用服务器端语言创建CSV文件,并将其作为下载服务(使用Content-disposition: attachment标头)。

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. 您可以通过将以下值(取决于下载的文件的扩展名)添加到注册表来修复它。

Eg for .csv file: 例如.csv文件:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.csv]
"PerceivedType"="document"

@MICROSOFT: Shame on you; @MICROSOFT:对你感到羞耻; problem seems to be still unpatched :-) 问题似乎仍未修补:-)

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做到这一点。

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

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