views:

705

answers:

6

I've been fiddling with WebGL lately, and have gotten a Collada reader working. Problem is it's pretty slow (Collada is a very verbose format), so I'm going to start converting files to a easier to use format (probably JSON). Thing is, I already have the code to parse the file in Javascript, so I may as well use it as my exporter too! The problem is saving.

Now, I know that I can parse the file, send the result to the server, and have the browser request the file back from the server as a download. But in reality the server has nothing to do with this particular process, so why get it involved? I already have the contents of the desired file in memory. Is there any way that I could present the user with a download using pure javascript? (I doubt it, but might as well ask...)

And to be clear: I am not trying to access the filesystem without the users knowledge! The user will provide a file (probably via drag and drop), the script will transform the file in memory, and the user will be prompted to download the result. All of which should be "safe" activities as far as the browser is concerned.

[EDIT]: I didn't mention it upfront, so the posters who answered "Flash" are valid enough, but part of what I'm doing is an attempt to highlight what can be done with pure HTML5... so Flash is right out in my case. (Though it's a perfectly valid answer for anyone doing a "real" web app.) That being the case it looks like I'm out of luck unless I want to involve the server. Thanks anyway!

+1  A: 

This is not possible in Javascript.
However, it can be done using Flash.

SLaks
+4  A: 

Take a look at Doug Neiner's Downloadify which is a Flash based JavaScript interface to do this.

Downloadify is a tiny JavaScript + Flash library that enables the generation and saving of files on the fly, in the browser, without server interaction.

Pekka
For most people, this is probably the answer that they'll need. So even though it doesn't meet my specific requirements (as explained above) I'm marking it as the accepted answer.
Toji
@Toji ah, I see. Maybe re-ask and re-phrase under the `HTML 5` banner and tag accordingly? That would be likely to attract those users who know about that specific field (still a comparably small crowd right now, I suppose). I'm pretty sure it can be done in HTML 5 but I have no idea how.
Pekka
+3  A: 

You can generate a data URI. However, there are browser-specific limitations.

Matthew Flaschen
This is interesting. I'll look into it more when I get a chance. Thanks!
Toji
i'd be interested in seeing if this approach works
Claudiu
A: 

Flash is the way to go dude.

Shubh
A: 

You can use localStorage. This is the Html5 equivalent of cookies. It appears to work on Chrome and Firefox BUT on Firefox, I needed to upload it to a server. That is, testing directly on my home computer didn't work.

I'm working up HTML5 examples. Go to http://faculty.purchase.edu/jeanine.meyer/html5/html5explain.html and scroll to the maze one. The information to re-build the maze is stored using localStorage.

I came to this article looking for HTML5 JavaScript for loading and working with xml files. Is it the same as older html and JavaScript????

Jeanine
A: 

Here is a link to the data URI method Mathew suggested, it worked on safari, but not well because I couldn't set the filetype, it gets saved as "unknown" and then i have to go there again later and change it in order to view the file...

http://www.nihilogic.dk/labs/canvas2image/

Dennkster