0

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.

9
  • Depends, where's the file located? Commented 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. Commented 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? Commented 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. Commented Aug 3, 2012 at 21:43
  • 1
    That 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?. Commented Aug 4, 2012 at 20:27

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

riiiight, but this will only work offline-- which if I gather is pretty much not useful for a website.
@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.
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
@FlavorScape Oops, I've mistaken you for the OP, sorry :p
@Rob W Is it possible to overwrite (instead of duplicating) a file using the method you described?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.