最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - execcommand("SaveAs",null,"file.csv") is not working in IE8 - Stack Overflow

programmeradmin1浏览0评论
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

but woks in IE 6

what is the problem ? it is alerting "cannot be saved"

help me !!! advance thanks

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

but woks in IE 6

what is the problem ? it is alerting "cannot be saved"

help me !!! advance thanks

Share Improve this question edited Mar 25, 2010 at 13:30 Andy E 345k86 gold badges480 silver badges450 bronze badges asked Mar 25, 2010 at 13:18 AnbuAnbu 9533 gold badges10 silver badges16 bronze badges 3
  • social.msdn.microsoft.com/Forums/en-NZ/iewebdevelopment/thread/… – jholster Commented Mar 25, 2010 at 13:33
  • 1 This is almost certainly a permissions problem. Where is this code coming from? In other words, what is the URL for the page that has this code in it? – Pointy Commented Mar 25, 2010 at 13:34
  • html table is in one html file & the above code is coming from the javascript file which is in javascript directory (/js/bug_list.js) – Anbu Commented Mar 25, 2010 at 13:39
Add a comment  | 

3 Answers 3

Reset to default 10

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:

This problem occurs because of a limitation in the ExecCommand function. 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.

Sure enough, change the file extension to ".txt" and watch it magically work in 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).

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:

Windows Registry Editor Version 5.00

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

@MICROSOFT: Shame on you; 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:

Content-Disposition: attachment; filename=file.csv

Which, unfortunately you can't do from JavaScript.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论