Prompting user to save a Javascript created text to a file

Discussion in 'Javascript' started by Jeff, Jan 17, 2007.

  1. Jeff

    Jeff Guest

    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
     
    Jeff, Jan 17, 2007
    #1
    1. Advertising

  2. Jeff

    Ivo Guest

    "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
     
    Ivo, Jan 17, 2007
    #2
    1. Advertising

  3. Jeff

    Bart Lateur Guest

    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.
     
    Bart Lateur, Jan 17, 2007
    #3
  4. Jeff

    VK Guest

    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."
     
    VK, Jan 17, 2007
    #4
  5. Jeff

    VK Guest

    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.
     
    VK, Jan 17, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmljaGFyZA==?=

    Prompting to Save Solution

    =?Utf-8?B?UmljaGFyZA==?=, Mar 10, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    344
    =?Utf-8?B?UmljaGFyZA==?=
    Mar 10, 2005
  2. =?Utf-8?B?a2lt?=
    Replies:
    3
    Views:
    7,600
    cbDevelopment
    Dec 15, 2005
  3. Replies:
    7
    Views:
    1,665
    Shunya
    Mar 1, 2006
  4. Douglas Dillon

    Prompting user for download in Servlet

    Douglas Dillon, Apr 1, 2005, in forum: Java
    Replies:
    2
    Views:
    546
    marcus
    Apr 2, 2005
  5. Richard Hollenbeck

    prompting to "save as" rather than opening images

    Richard Hollenbeck, Jan 12, 2004, in forum: Javascript
    Replies:
    5
    Views:
    122
Loading...

Share This Page