HTML5 - file question?

Associate
Joined
25 Feb 2006
Posts
194
HTML5 - local storage?

Anyone know how to create and write a simple text file using javascript from a web browser? The file needs to be stored locally on the client. Whats the easiest way? I dont mind if it doesnt work in all browsers. Thanks for any help!
 
Last edited:
Anyone know how to create and write a simple text file using javascript from a web browser? The file needs to be stored locally on the client. Whats the easiest way? I dont mind if it doesnt work in all browsers. Thanks for any help!

There are a few options for this - it depends exactly on what you want to do, but most likely you'll want to look at local storage - it has very good support from the major browsers.

Check out html5 rocks for info on all the options: http://www.html5rocks.com/en/features/storage and be aware that the filesystem api only works on Chrome right now and is a bit glitchy (from personal experience!).
 
There are a few options for this - it depends exactly on what you want to do, but most likely you'll want to look at local storage - it has very good support from the major browsers.

Thanks but "local storage" seems to only store key-value pairs in a mini-database which can only be accessed by the same browser.

Is there a way of getting the browser to write a text file in the users file-system? (I'm trying to get some data out of a browser into another application)
 
Last edited:
I'm not certain, but I don't think javascript alone can be used to create a file on the client. JS's client access is heavily restricted, for obvious reasons.

I think you may be able to create a file client-side using flash. You could also use a server-side language to create a file with the relevent MIME type, although this would present the user with a download/save dialog, and wouldn't automatically place it in a specific folder on their machine.
 
Have a look at the FileSystem API for HTML5 (Chrome only); gives you limited/sandboxed access anyway.
Otherwise it's HTML5 Localstorage or WebSQL, or cookies, Flash/Java filesystem.
 
Anyone know how to create and write a simple text file using javascript from a web browser? The file needs to be stored locally on the client. Whats the easiest way? I dont mind if it doesnt work in all browsers. Thanks for any help!

Can I ask what exactly is it that you want to write to this file and what is it being used for?

For me understanding the reason why you want to do something may lead to giving a better/alternative solution if one is available.


Is the file just to remember something that the user is doing while they are on the site or is it being used for more than that? (could a cookie not be used as an alternative)

edit: sorry saw that you wanted to access this information from another application. Could you not just push the data onto the clipboard?

Is this going to be accessed via a windows app? if so could you not just host the browsing session in a browser control and get the information for that?
 
Last edited:
Can I ask what exactly is it that you want to write to this file and what is it being used for?

Yes of course. There are three types of file I want to create for my university projects, but I'm having trouble finding the best way to do it..

(1) ".dxf" - text file readable by CAD systems. My HTML5/Javascript software would generate the content on the client. The dxf file could then be taken by usb-stick to a laser cutter, for example, or opened in a CAD system.
(2) ".csv" - comma separated variables, for reading by Excel. The Javascript would create the content - these would be results of some scientific simulations.
(3) ".txt" - general bits and pieces, so can be opened by Notepad or whatever

I dont need to save binary files, just plain old ASCII.

Thanks!!
 
Last edited:
All you can do is provide a stream to a file that the user will need to manually save.

Hows that done? Supposing my client Javascript is just

var mycontent = new String("hello mum");

How does the user save mycontent into a file called "anyoldname.dxf" ?
I'm happy for the user to pick anyoldname and location, but the filename needs to end with .dxf
 
Last edited:
You cannot enforce it. Sorry to disappoint. The only reliable way to serve a file to someone through a web-browser is from a server, not in JS. See above link for a hack to serve a file from/in JS, but even that is unsupported in most browsers afaik.
 
Basically you can't really do what you are trying to do - I did once use the browser as a tool which wrote the output to the local filesystem (using the filesystem api) but it's not really the way to go - it only gives you access to a sandbox and obfuscates all the file names. In the end, I just ended up porting all my javascript to python! :p
 
Its possible for the user to save a local file on the client in Silverlight, which I've done previously, but I'm looking for a way of doing the same thing in native HTML5/JS for a uni project. But I guess the answer is no.

Someone said send it to the server and use a MIME type to download it and save on the client, but does anyone know how to do that?
 
Last edited:
Dude.. how many times? You *CANNOT* do this with client-side scripting. You must either A) write an application that will be installed and run ON the client machine, or B) Allow the user to manually save a file from your web/file server.

The security risks are *HUGE* if JS were able to do this. Just for laughs I could create a site that when opened by anyone overwrites, say, "%WINDIR%\explorer.exe" or other important file on the user's machine with my own variant.
 
Last edited:
Someone said send it to the server and use a MIME type to download it and save on the client, but does anyone know how to do that?
In ASP.Net MVC it's simple as returning a string as a byte array to a FileContentResult. In PHP/ASP you can set a specific MIME type header for the page.

As I said though in my first reply, that will open up the save/open dialog on the client's machine, it won't guarante where they download the file to, or even that they download it at all.

*edit*
And you also won't be able to read the file from the client machine unless they upload it to the server - should you be planning something like that as well.
 
Last edited:
Back
Top Bottom