0

I was wondering how do I put HTML form user input into a file using JavaScript ONLY. I have struggled to find an answer to such a simple question.

7
  • .... like write to an external file? Or the same file? Commented Mar 27, 2013 at 7:24
  • Yes that is exactly what i mean Gnuey, an Externeal file. Commented Mar 27, 2013 at 7:25
  • To a local file on the user's computer or a file on the server? Commented Mar 27, 2013 at 7:27
  • A file on the server. Commented Mar 27, 2013 at 7:30
  • But are you using some form of JavaScript server-side? Commented Mar 27, 2013 at 7:37

1 Answer 1

2

Writing data to files on a local filesystem is only supported in modern browsers with a set of limitations, you can google for HTML FileSystem API.

As for writing to a file, this is a basic example:

function onInitFs(fs) {

  fs.root.getFile('log.txt', {create: true}, function(fileEntry) {

    // Create a FileWriter object for our FileEntry (log.txt).
    fileEntry.createWriter(function(fileWriter) {

      fileWriter.onwriteend = function(e) {
        console.log('Write completed.');
      };

      fileWriter.onerror = function(e) {
        console.log('Write failed: ' + e.toString());
      };

      // Create a new Blob and write it to log.txt.
      var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});

      fileWriter.write(blob);

    }, errorHandler);

  }, errorHandler);

}

window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, It isn't working. Thank you for trying to help though :)
@LittleProgrammer, when someone makes a big effort writing a high quality answer, cant you at least mention in what ways its not working?
No worries, I just know the feeling when your answers are dismissed without an explanation. I see now that the effort maybe wasn't as big as I thought, as it was an example pasted from somewhere :) I was on my phone before and didn't read properly. Obviously you'd have to adapt his example to your situation, but if things break simply by including his code I guess it's a bad sign.
yes, this is a basic piece of code, which I took on the Internet, but it is easily adaptable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.