Solved

Saving a CSV file using execcommand()

Posted on 2004-04-12
8
2,390 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:alison92
  • 4
  • 4
8 Comments
 

Author Comment

by:alison92
ID: 10808429
My script is very simple, essentially:

function ExportCsv()
{
     document.execCommand('SaveAs', true, <url of the csv file>);
}

<a href="#" onClick="ExportCsv();"></a>
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10808585
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.execCommand('SaveAs', true, <url of the csv file>);
}
0
 

Author Comment

by:alison92
ID: 10808981
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.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 12

Expert Comment

by:lil_puffball
ID: 10809020
This code works for me:

<script>
function ExportCsv(){
  window.frames["iframeName"].document.execCommand("SaveAs",true);
}
</script>

<a href="#" onclick="ExportCsv();">Export</a>

<iframe style="display:none;" name=iframeName src=test.htm></iframe>

Perhaps it's different for CSV files?
0
 

Author Comment

by:alison92
ID: 10809054
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.
0
 
LVL 12

Accepted Solution

by:
lil_puffball earned 500 total points
ID: 10809159
Hmmm..I see the problem.

I'm not sure if it's possible. Can you just have a link to the file, and tell the user to right click it and save it that way?
0
 

Author Comment

by:alison92
ID: 10813689
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.
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10817013
Thanks for the points. :) Sorry I couldn't help more. :( I'll let you know if I find anything useful too.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to make this slider behave as it should with javascript 5 28
how can i select 4 29
Html5 Index on a table 7 26
Need Help adding an array and loop to a React file 7 27
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question