Prompting user to save a Javascript created text to a file

J

Jeff

Hi,

I'd like to be able to prompt the user to save text that a script has
created to a file.

The idea is that the script would create a text string, the "Save As"
dialog would be triggered and then the user could choose to save the
text that was created somewhere on their local filesystem. This has
none of the security concerns of Javascript being able to write to the
local filesystem but still allows me to customize data to the user.

With Firefox, doing the following almost works, but I haven't figured
out a way to suggest a default filename (Firefox, uses what appears to
be a random string). IE croaks on it.

window.location = "data:plain/text,Test";

This prompts me to save the file and when I do the string "Test" is
contained within the file.

Does anyone know how to do this the right way? Hopefully, also in a
cross-browser way?

Thanks!
Jeff
 
I

Ivo

I'd like to be able to prompt the user to save text that a script has
created to a file.
With Firefox, doing the following almost works, but I haven't figured
out a way to suggest a default filename (Firefox, uses what appears to
be a random string). IE croaks on it.

window.location = "data:plain/text,Test";

This prompts me to save the file and when I do the string "Test" is
contained within the file.

Interesting, I 'll have to try that out.
For IE, see
http://4umi.com/web/javascript/filewrite.htm
HTH
Ivo
 
B

Bart Lateur

Jeff said:
With Firefox, doing the following almost works, but I haven't figured
out a way to suggest a default filename (Firefox, uses what appears to
be a random string). IE croaks on it.

window.location = "data:plain/text,Test";

This prompts me to save the file and when I do the string "Test" is
contained within the file.

You made me wonder about this. I figured that the plain/text was
actually a mangled MIME content type. Indeed, replacing it with
"text/plain" makes the browser show it; replacing it with "text/html"
makes the browser show the data as HTML.

I went Googling, and found a page in Wikipedia:

Data: URI scheme
<http://en.wikipedia.org/wiki/Data:_URI_scheme>

I fine starting point for any further queries. Like workarounds for MSIE
that according to the article still doesn't handle it.
 
V

VK

Jeff said:
With Firefox, doing the following almost works, but I haven't figured
out a way to suggest a default filename (Firefox, uses what appears to
be a random string). IE croaks on it.

window.location = "data:plain/text,Test";

This prompts me to save the file and when I do the string "Test" is
contained within the file.

You've made me worried for a sec by your discover:
<https://bugzilla.mozilla.org/show_bug.cgi?id=367231>

Sorry, nothing personal, just a quick security check. As you may see it
is not a bug but an enhancement request now.

Just in case I quote the clarification about the file name:
"since it wasn't loaded from a particular path from which we could take
a file name, and because it's not an HTML document where we could use
the <title>, we pick a unique temporary name.
Because it's confusing you could make the case for an enhancement
request that we should prompt the user for a filename when we don't
have one rather than pick something random."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top