alison92
asked on
Saving a CSV file using execcommand()
Hi,
Right now, I'm trying to get a SaveAs dialog box to display and save a CSV file when they click on a link.
I have been using the execcommand() at many unsuccessful attempts at this.
My question is, is it necessary to for the contents of the CSV file to be within the page of the JavaScript itself before I could use execcommand() to prompt the user to save it?
Thanks.
Right now, I'm trying to get a SaveAs dialog box to display and save a CSV file when they click on a link.
I have been using the execcommand() at many unsuccessful attempts at this.
My question is, is it necessary to for the contents of the CSV file to be within the page of the JavaScript itself before I could use execcommand() to prompt the user to save it?
Thanks.
Yes, the page has to be loaded.
You can try putting the page in a hidden Iframe:
<iframe name=iframeName style="display:none;" src=<url of the csv file>></iframe>
And then:
function ExportCsv()
{
window.frames["iframeName" ].document .execComma nd('SaveAs ', true, <url of the csv file>);
}
You can try putting the page in a hidden Iframe:
<iframe name=iframeName style="display:none;" src=<url of the csv file>></iframe>
And then:
function ExportCsv()
{
window.frames["iframeName"
}
ASKER
I tried the code above and it doesn't work. I am, however, setting the url of the csv file in my code for both the iframe and the JavaScript function.
When I click on my export link, it just brings me to a different page, instead of prompting me with the SaveAs dialog box.
When I click on my export link, it just brings me to a different page, instead of prompting me with the SaveAs dialog box.
This code works for me:
<script>
function ExportCsv(){
window.frames["iframeName" ].document .execComma nd("SaveAs ",true);
}
</script>
<a href="#" onclick="ExportCsv();">Exp ort</a>
<iframe style="display:none;" name=iframeName src=test.htm></iframe>
Perhaps it's different for CSV files?
<script>
function ExportCsv(){
window.frames["iframeName"
}
</script>
<a href="#" onclick="ExportCsv();">Exp
<iframe style="display:none;" name=iframeName src=test.htm></iframe>
Perhaps it's different for CSV files?
ASKER
I did a test using .txt and .htm/l files. They work for both, but it doesn't work for the type of file that I'd like to save. Would you be able to suggest anything else? I understand that execcommand() is also an IE thing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have it like that, but it's an added step in the workflow, so it would be preferable if I could have them left-click and the SaveAs dialog box come up right away.
I'll experiment with Response.content-type and IIS settings and such to see what I could come up with. Thank you for your help.
I'll experiment with Response.content-type and IIS settings and such to see what I could come up with. Thank you for your help.
Thanks for the points. :) Sorry I couldn't help more. :( I'll let you know if I find anything useful too.
ASKER
function ExportCsv()
{
document.execCommand('Save
}
<a href="#" onClick="ExportCsv();"></a