levivanzele
levivanzele

Reputation: 736

Read file from local filesystem using JS and/or HTML5

I was wondering, is it possible to read/write a file from/to a certain user directory using JavaScript and/or HTML5? E.g. a user fills in %USERPROFILE%\Documents\mydata.svg or ~/Documents/mydata.svg into a textfield, and the file is written.

Or would I have to be forced use a Java applet to do this?

Sidenote #1: If the solution only works in Google Chrome, that's fine; but any webkit browser would be even better.

Sidenote #2: The location doesn't really matter, as long as it's the same location every time. It might as well be {sandboxed dir}\mydata.svg in this case, and then the user just doesn't get an option where his/her data is stored.

Upvotes: 1

Views: 3311

Answers (3)

Chris Baker
Chris Baker

Reputation: 50592

In unprivileged content: no, not under any circumstances*. Good thing, too, or your computer would be rendered useless after 10 minutes on the internet.

If an extension would suffice, check out some related answers here: Writing to local file system in Chrome extension or Chrome extension: How to save a file on disk

Best practice would be to offer the SVG file as a download and let the user decide if and where to save it: http://www.benbarnett.net/2010/06/04/export-svg-from-raphael-js-to-create-a-png-bitmap/

(* - the File API is in draft and may be widely available in the future, but is currently only supported in Chrome. )

Upvotes: 2

Bengel
Bengel

Reputation: 1053

Take a look at Google Chrome's FileSystem Api.

I've always thought it would be handy if web applications could read and write files and directories. As we move from offline to online, applications are becoming more complex and the lack of file system APIs has been a hindrance for moving the web forward. Storing or interacting with binary data shouldn't be limited to the desktop. Thankfully, it no longer is thanks to the FileSystem API. With the FileSystem API, a web app can create, read, navigate, and write to a sandboxed section of the user's local file system.

Upvotes: 0

Madara's Ghost
Madara's Ghost

Reputation: 174937

No, as that would allow you to invade the user's privacy by writing malicious software onto his computer. You can generate the SVG and offer it for the user to download though.

Upvotes: 1

Related Questions