简体   繁体   中英

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? Eg 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.

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. )

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM