简体   繁体   中英

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

but woks in 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 :

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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