Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

create a dowloadable csv file in a javascript

224 views
Skip to first unread message

Pif

unread,
Nov 26, 2009, 4:20:43 AM11/26/09
to
Hello,

I make calculations and many thinks in a page that result in a
javascript table file. I know how to create a CSV string from this
table in JS, but I would like that when the user click on a given
button, the script launchs and generate a file that is dowloadable
using a dialog box without requesting server.

Can you tell me if it is possible and how I can do that ?

This must be compatible with IE6 (and higher) and latest release of
FF.

Thanks a lot for you help !

Richard Cornford

unread,
Nov 26, 2009, 8:47:58 AM11/26/09
to
On Nov 26, 9:20 am, Pif wrote:
> I make calculations and many thinks in a page that
> result in a javascript table file. I know how to create
> a CSV string from this table in JS, but I would like that
> when the user click on a given button, the script launchs
> and generate a file that is dowloadable using a dialog
> box without requesting server.
>
> Can you tell me if it is possible and how I can do that ?

It is not impossible, using data URLs.

> This must be compatible with IE6 (and higher) and latest
> release of FF.

IE 6 has no support for data URLs, so it looks like you will need to
use server-side code.

Richard.

Jorge

unread,
Nov 26, 2009, 5:25:49 PM11/26/09
to
On Nov 26, 2:47 pm, Richard Cornford <Rich...@litotes.demon.co.uk>
wrote:

>
> IE 6 has no support for data URLs, so it looks like you will need to
> use server-side code.

<meta http-equiv="X-UA-Compatible" content="chrome=1">

http://code.google.com/chrome/chromeframe/faq.html
--
Jorge

Michael Haufe ("TNO")

unread,
Nov 26, 2009, 5:54:22 PM11/26/09
to
On Nov 26, 7:47 am, Richard Cornford <Rich...@litotes.demon.co.uk>
wrote:

> IE 6 has no support for data URLs, so it looks like you will need to
> use server-side code.

In IE6 you can use document.execCommand("SaveAs", null, "FileName");

Here an example for saving files in IE 6:

http://4umi.com/web/javascript/filewrite.php?txt=This+is+some+sample+text.%0D%0AThis+is+a+new+line.&filename=C%3A%5CMy+Documents%5Cfilename&ext=utf-8#

For the OP, here's an example of prompting a download with a data uri:

http://thenewobjective.com/temp/save.html

For a plain text file, the uri will look something like this:
"data:text/octet-stream," + FileContents

Garrett Smith

unread,
Nov 26, 2009, 6:36:26 PM11/26/09
to
Michael Haufe ("TNO") wrote:
> On Nov 26, 7:47 am, Richard Cornford <Rich...@litotes.demon.co.uk>
> wrote:
>
>> IE 6 has no support for data URLs, so it looks like you will need to
>> use server-side code.
>
> In IE6 you can use document.execCommand("SaveAs", null, "FileName");
>

[snip]

Good one!

Next step is to take both and create a cross-browser function and test
it in as many browsers.

Questions about your code:
1) why use - document.body.insertBefore( w ); - instead of appendChild?
3) why use window.open?
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/

Garrett Smith

unread,
Nov 26, 2009, 6:41:52 PM11/26/09
to
Michael Haufe ("TNO") wrote:
> On Nov 26, 7:47 am, Richard Cornford <Rich...@litotes.demon.co.uk>
> wrote:
>
>> IE 6 has no support for data URLs, so it looks like you will need to
>> use server-side code.
>
> In IE6 you can use document.execCommand("SaveAs", null, "FileName");
>
> Here an example for saving files in IE 6:
>
> http://4umi.com/web/javascript/filewrite.php?txt=This+is+some+sample+text.%0D%0AThis+is+a+new+line.&filename=C%3A%5CMy+Documents%5Cfilename&ext=utf-8#

[snip]

The MSDN links in your article are broken. The correct links should be:

http://msdn.microsoft.com/en-us/library/ms536419%28VS.85,loband%29.aspx
http://msdn.microsoft.com/en-us/library/ms537418%28VS.85%29.aspx

Thomas 'PointedEars' Lahn

unread,
Nov 26, 2009, 7:02:20 PM11/26/09
to
Michael Haufe ("TNO") wrote:

> Richard Cornford wrote:
>> IE 6 has no support for data URLs, so it looks like you will need to
>> use server-side code.
>
> In IE6 you can use document.execCommand("SaveAs", null, "FileName");
>
> Here an example for saving files in IE 6:
>
> http://4umi.com/web/javascript/filewrite.php?txt=This+is+some+sample+text.
%0D%0AThis+is+a+new+line.&filename=C%3A%5CMy+Documents%5Cfilename&ext=utf-8#

Joe User is not going to be able to type or to copy-paste the target file
path, though.

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

Michael Haufe ("TNO")

unread,
Nov 26, 2009, 7:49:19 PM11/26/09
to
On Nov 26, 5:36 pm, Garrett Smith <dhtmlkitc...@gmail.com> wrote:

> Good one!
>
> Next step is to take both and create a cross-browser function and test
> it in as many browsers.
>
> Questions about your code:
> 1) why  use - document.body.insertBefore( w ); - instead of appendChild?
> 3) why use window.open?


The 4umi.com website is not my domain, only the thenewobjective.com
domain is.

kangax

unread,
Nov 28, 2009, 8:25:43 PM11/28/09
to
Garrett Smith wrote:
> Michael Haufe ("TNO") wrote:
>> On Nov 26, 7:47 am, Richard Cornford <Rich...@litotes.demon.co.uk>
>> wrote:
>>
>>> IE 6 has no support for data URLs, so it looks like you will need to
>>> use server-side code.
>>
>> In IE6 you can use document.execCommand("SaveAs", null, "FileName");
>>
>
> [snip]
>
> Good one!
>
> Next step is to take both and create a cross-browser function and test
> it in as many browsers.
>
> Questions about your code:
> 1) why use - document.body.insertBefore( w ); - instead of appendChild?

Perhaps, to avoid IE's "Operation aborted" error?
<http://support.microsoft.com/kb/927917>

--
kangax

0 new messages