views:

752

answers:

4

Hi All, I need a Javascript sample which reads contents from a url and creates a file in the client with same contents when some button is clicked. I also understand that Javascript cannot be allowed to access the local file system (Unless you use ActiveX objects). I do not want to rely on ActiveX objects (since my client is not IE always).

So here is what I did. I used the standard XmlHttpRequest made a request and got my bytes. Now I thought I could kinda stream this contents to the user, first by opening a standard dialog box (the box that pops up when you attempt download something from internet with options like Open/Save/Cancel) and then asking the user to save it somewhere.

I know how to do the read part, can someone show some javascript/html sample on "How to stream open a confirm dialog box to the user and stream some contents?"

PS: Not too sure if this could be done in javascript at all but with Javascript you never know what is possible and what is not :)

A: 

If I understand what your question is, then you want to use JavaScript to write data like a server side script would (php, python, java, etc..) but in browser?

If so, then what your asking isn't possible with in browser JavaScript.

However if for some reason you wanted to do this with server side JavaScript, then yes the "streaming" part is possible.

Brian Gianforcaro
A: 

See it this way: if you could read/write to users' computers using JavaScript, then no computer would be safe from browsing the web.

Having said so, you cannot read/write client-side files using JavaScript.

But you could if you use Flash / a Java applet; in those cases the embedded objects asks for your permission before doing such actions.

Seb
+1  A: 

Rather than using Javascript to stream the content, which is not possible for obvious security reasons, you need to point the browser at a URL that will return a 'Content-Type' header of 'application/octet-stream'. In most cases this will force the browser to initiate a 'save as' operation and ask the user what to do with it.

I believe it is possible to do this using an iframe in the same page, such that the user will not have to navigate away from the page or open a new tab/window.

roryf
A: 

You may want to have a look at TiddlyWiki that claims it can write itself to the disk...

Vincent Robert