469,604 Members | 2,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,604 developers. It's quick & easy.

Prompting user to save a Javascript created text to a file

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

Jan 17 '07 #1
4 3462
Ivo
"Jeff" wrote
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
Jan 17 '07 #2
Jeff wrote:
>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.
--
Bart.
Jan 17 '07 #3
VK
Jeff wrote:
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."

Jan 17 '07 #4
VK

VK wrote:
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.
To make it all clear: I pointed to the exact founder not for some
"responsibility shift" or so. But in case if it was confirmed as a
direct or potential security risk you would be the one entitled for the
US $500 cash bonus from Mozilla.

Jan 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by George Hester | last post: by
5 posts views Thread by Phillip N Rounds | last post: by
11 posts views Thread by uoL | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.