I'm trying to write a simple client-side text editor using HTML5 and Javascript. Is it possible to overwrite the contents of an existing text file using HTML5? I know that the HTML File API makes it possible to read the contents of a file, but I haven't yet found a way to modify or overwrite an existing file.
-
Depends, where's the file located?Rob W– Rob W2012-08-03 21:39:55 +00:00Commented Aug 3, 2012 at 21:39
-
I'd like to prompt the user to select a file to open, and then modify and overwrite the file.Anderson Green– Anderson Green2012-08-03 21:41:21 +00:00Commented Aug 3, 2012 at 21:41
-
Judging by that comment, you don't care where the file is saved, as long as it's saved on prompt. Am I correct?Rob W– Rob W2012-08-03 21:42:39 +00:00Commented Aug 3, 2012 at 21:42
-
Yes, but I'd like to select a file to open, and then modify the contents of the file.Anderson Green– Anderson Green2012-08-03 21:43:38 +00:00Commented Aug 3, 2012 at 21:43
-
1That is a virtual filesystem. For more information, see tagwiki of html5-filesystem and Where is the filesystem in html5 stored on the real file system?.Rob W– Rob W2012-08-04 20:27:01 +00:00Commented Aug 4, 2012 at 20:27
|
Show 4 more comments
1 Answer
Prompt to open
To read a file, use the FileReader API (examples):
Prompt to save
To save the file, create an URL using URL.createObjectURL (the blob is constructed via the FileReader API, with type application/octet-stream), or using a data-URI (example).
6 Comments
FlavorScape
riiiight, but this will only work offline-- which if I gather is pretty much not useful for a website.
Rob W
@FlavorScape Hang on, do you want the client to browse on the server using pure client-side JavaScript? If yes, that's not possible, because the client can't browse/modify server-side files without a server-side implementation. The methods suggested by my answer allows a user to select an accessible file from their computer, do some magic within the web page, then save the file on their computer.
FlavorScape
oh ok-- i guess that's not a requirement. i just assumed that since the OP used the word "client-side" that there was a "server-side" somewhere
Rob W
@FlavorScape Oops, I've mistaken you for the OP, sorry :p
Anderson Green
@Rob W Is it possible to overwrite (instead of duplicating) a file using the method you described?
|