Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execcommand("SaveAs",null,"file.csv") is not working in IE8

Tags:

javascript

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

like image 262
Anbu Avatar asked Mar 25 '10 13:03

Anbu


2 Answers

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).

like image 99
Andy E Avatar answered Sep 24 '22 13:09

Andy E


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 :-)

like image 26
Supagusti Avatar answered Sep 22 '22 13:09

Supagusti