tags:

views:

342

answers:

2
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

+2  A: 

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

Andy E
very thanks for your answering
Anbu
As you said it is working when we save it as "txt". if you guys find any trick then you can post the idea as i have to be done it from client side!! advance thanks
Anbu
http://4umi.com/web/javascript/filewrite.php check out this link and if possible help me
Anbu
@Anbu: are you running the page from the local filesystem and only in IE? If so, it's possible you could use an ActiveX control (namely, FileSystemObject) to write the CSV file. See http://msdn.microsoft.com/en-us/library/6ee7s9w2(VS.85).aspx for more info.
Andy E
A: 

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.

Matt Blaine
thanks for the timly reply!! but i cant do anything in serverside so please give a solution in client side. Is there any specific reason that it will not work in IE8 ?
Anbu
Try Andy E's suggestion, change the file extension to txt.
Matt Blaine