Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #21247 > unrolled thread

Write variables to file?

Started by"Robert Crandal" <rcranz143101@gmail.com>
First post2013-09-10 03:10 -0700
Last post2013-09-10 12:31 -0500
Articles 8 — 7 participants

Back to article view | Back to comp.lang.javascript


Contents

  Write variables to file? "Robert Crandal" <rcranz143101@gmail.com> - 2013-09-10 03:10 -0700
    Re: Write variables to file? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-10 10:24 +0000
    Re: Write variables to file? Richard Maher <maher_rjSPAMLESS@hotmail.com> - 2013-09-10 18:42 +0800
    Re: Write variables to file? Martin Honnen <mahotrash@yahoo.de> - 2013-09-10 12:45 +0200
    Re: Write variables to file? JJ <duh@nah.meh> - 2013-09-10 18:13 +0700
      Re: Write variables to file? Ken <usenet-so@abdiassoftware.com> - 2013-09-10 12:15 -0500
      Re: Write variables to file? dhtml <dhtmlkitchen@gmail.com> - 2013-09-30 00:44 -0700
    Re: Write variables to file? Ken <usenet-so@abdiassoftware.com> - 2013-09-10 12:31 -0500

#21247 — Write variables to file?

From"Robert Crandal" <rcranz143101@gmail.com>
Date2013-09-10 03:10 -0700
SubjectWrite variables to file?
Message-ID<8eqdnQgNPt5ncrPPnZ2dnUVZ5hSdnZ2d@giganews.com>
Can I use Javascript to write data into a text file on the
client machine?  I tried experiement with cookie objects, but this
didn't produce the results that I wanted.

My webpage will display a form with text fields.  The user
will type in data.  I then want to save this data somehow on
the client machine.

Is this possible??  This is probably a dumb question, but I
wanted to ask anyways since I'm pretty much a Javascript
newbie.

Thanks!

[toc] | [next] | [standalone]


#21248

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-09-10 10:24 +0000
Message-ID<l0ms11$5c1$1@dont-email.me>
In reply to#21247
On Tue, 10 Sep 2013 03:10:02 -0700, Robert Crandal wrote:

> Can I use Javascript to write data into a text file on the client
> machine?  I tried experiement with cookie objects, but this didn't
> produce the results that I wanted.
> 
> My webpage will display a form with text fields.  The user will type in
> data.  I then want to save this data somehow on the client machine.

You can save it as a cookie on the client machine, but not as a normal 
file.

This means that access to the data on the client machine has to be done 
either through, or with knowledge of, the browser's cookie storing 
methods.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#21249

FromRichard Maher <maher_rjSPAMLESS@hotmail.com>
Date2013-09-10 18:42 +0800
Message-ID<l0mt1t$f7g$1@speranza.aioe.org>
In reply to#21247
On 9/10/2013 6:10 PM, Robert Crandal wrote:
> Can I use Javascript to write data into a text file on the
> client machine?  I tried experiement with cookie objects, but this
> didn't produce the results that I wanted.
>
> My webpage will display a form with text fields.  The user
> will type in data.  I then want to save this data somehow on
> the client machine.
>
> Is this possible??  This is probably a dumb question, but I
> wanted to ask anyways since I'm pretty much a Javascript
> newbie.
>
> Thanks!
>
>
HTML5 WebStorage may fit the bill?
Eg: - http://www.html5rocks.com/en/features/storage

Seems to be on by default in most browsers but I turn it off so that 
newspapers stop asking me to pay :-)

(Yes, private/incognito sessions are better but FWIW good-luck.)

Cheers Richard Maher

PS. I think you may be looking for a signed Applet here but I'm being 
trolled so much lately I don't care to defend the idea. (But I strongly 
suggest that you consider it!)

[toc] | [prev] | [next] | [standalone]


#21250

FromMartin Honnen <mahotrash@yahoo.de>
Date2013-09-10 12:45 +0200
Message-ID<522ef835$0$6584$9b4e6d93@newsspool3.arcor-online.net>
In reply to#21247
Robert Crandal wrote:
> Can I use Javascript to write data into a text file on the
> client machine?  I tried experiement with cookie objects, but this
> didn't produce the results that I wanted.
>
> My webpage will display a form with text fields.  The user
> will type in data.  I then want to save this data somehow on
> the client machine.
>
> Is this possible??

There is a newer feature than cookies, see 
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage.

[toc] | [prev] | [next] | [standalone]


#21251

FromJJ <duh@nah.meh>
Date2013-09-10 18:13 +0700
Message-ID<kl0ldr6xnued.1e5nvapifsd1h$.dlg@40tude.net>
In reply to#21247
On Tue, 10 Sep 2013 03:10:02 -0700, Robert Crandal wrote:
> Can I use Javascript to write data into a text file on the
> client machine?  I tried experiement with cookie objects, but this
> didn't produce the results that I wanted.
> 
> My webpage will display a form with text fields.  The user
> will type in data.  I then want to save this data somehow on
> the client machine.
> 
> Is this possible??  This is probably a dumb question, but I
> wanted to ask anyways since I'm pretty much a Javascript
> newbie.
> 
> Thanks!

Easiest metod is to use data URI to save the data, but there is no way to
provide the file name. The user will have to type the file name manually
(e.g.: "Form Data.txt"). The code is like this:

var inp = document.getElementById("theInputElement");
var data = btoa(inp.value);
var dataURI = "data:text/raw;charset=utf-8;base64," + data;
location = dataURI;

Or, you can use DOM File API. More complicated, but the file name can be
provided.

https://developer.mozilla.org/en-US/docs/DOM/File_APIs

[toc] | [prev] | [next] | [standalone]


#21253

FromKen <usenet-so@abdiassoftware.com>
Date2013-09-10 12:15 -0500
Message-ID<l0nk4t$kmm$3@speranza.aioe.org>
In reply to#21251
On 10.09.2013 06:13, JJ wrote:> On Tue, 10 Sep 2013 03:10:02 -0700, 
Robert Crandal wrote:
>> Can I use Javascript to write data into a text file on the
>> client machine?  I tried experiement with cookie objects, but this

> Easiest metod is to use data URI to save the data, but there is no way to
> provide the file name. The user will have to type the file name manually

Actually you can provide a file name - sort of. You can use an A element 
with the new *download* attribute:

   a href="data:..." download="myFilename.txt">Click to download</a


-- 
Ken Fyrstenberg
Abdias Software
http://abdiassoftware.com/
(remove usenet- from address)

[toc] | [prev] | [next] | [standalone]


#21473

Fromdhtml <dhtmlkitchen@gmail.com>
Date2013-09-30 00:44 -0700
Message-ID<d2d2fce0-15b5-48f8-aa4e-fa4822ee5ad9@googlegroups.com>
In reply to#21251
On Tuesday, September 10, 2013 4:13:32 AM UTC-7, JJ wrote:
> On Tue, 10 Sep 2013 03:10:02 -0700, Robert Crandal wrote:
> 
> > Can I use Javascript to write data into a text file on the
> 
> > client machine?  I tried experiement with cookie objects, but this
> 
> > didn't produce the results that I wanted.
> 
> > 
> 
> > My webpage will display a form with text fields.  The user
> 
> > will type in data.  I then want to save this data somehow on
> 
> > the client machine.
> 
> > 
> 
> > Is this possible??  This is probably a dumb question, but I
> 
> > wanted to ask anyways since I'm pretty much a Javascript
> 
> > newbie.
> 
> > 
> 
> > Thanks!
> 
> 
> 
> Easiest metod is to use data URI to save the data, but there is no way to
> 
> provide the file name. The user will have to type the file name manually
> 
> (e.g.: "Form Data.txt"). The code is like this:
> 
> 
> 
> var inp = document.getElementById("theInputElement");
> 
> var data = btoa(inp.value);
> 
> var dataURI = "data:text/raw;charset=utf-8;base64," + data;
> 
> location = dataURI;
> 
> 
> 
> Or, you can use DOM File API. More complicated, but the file name can be
> 
> provided.
> 
> 
> 
> https://developer.mozilla.org/en-US/docs/DOM/File_APIs

Where is FileWriter supported, if anywhere?
http://www.w3.org/TR/file-writer-api/#idl-def-FileWriter

We discussed here before, but I forgot the thread. This explains one way using octet-stream. I think there were some problems with it but I forgot what they were.
http://stackoverflow.com/questions/11631749/using-data-uris-to-prompt-the-user-to-save-files

[toc] | [prev] | [next] | [standalone]


#21254

FromKen <usenet-so@abdiassoftware.com>
Date2013-09-10 12:31 -0500
Message-ID<l0nl37$ovm$1@speranza.aioe.org>
In reply to#21247
On 10.09.2013 05:10, Robert Crandal wrote:> Can I use Javascript to 
write data into a text file on the
 > client machine?  I tried experiement with cookie objects, but this
 > didn't produce the results that I wanted.
 >
 > My webpage will display a form with text fields.  The user
 > will type in data.  I then want to save this data somehow on
 > the client machine.

Simple answer: no.

Imagine what harm a "bad hacker" could do if you could write directly to 
a user's ordinary file system.

All the direct storage mechanims in a browser are sand-boxed, being Web 
Storage (localStorage and sessionStorage), File API (currently only 
supported in Chrome), Indexed DB or WebSQL (latter now deprecated but 
still widely supported).

You can however provide the text file as a data-uri and make it 
available from a link with the new download attribute set so when a user 
clicks the link a save as file requester will appear allowing user to 
save the file initially set with the name you set.

You can encode data-uri's as Base64 or printed-quotable. You can also 
use a Blob object to retrieve a link from.

You can create this element dynamically using document.createElement() 
(and hint: you can also produce a click event for it) or simply set the 
download and href attributes on an existing a element at run-time.


-- 
Ken Fyrstenberg
Abdias Software
http://abdiassoftware.com/
(remove usenet- from address)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web