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.

JJJ
  • 32,902
  • 20
  • 89
  • 102

1 Answers1

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);
Valentin V
  • 24,971
  • 33
  • 103
  • 152
  • Sorry, It isn't working. Thank you for trying to help though :) – LittleProgrammer Mar 27 '13 at 07:37
  • @LittleProgrammer, when someone makes a big effort writing a high quality answer, cant you at least mention in what ways its not working? – Peter Herdenborg Mar 27 '13 at 07:42
  • 1
    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. – Peter Herdenborg Mar 27 '13 at 08:08
  • yes, this is a basic piece of code, which I took on the Internet, but it is easily adaptable. – Valentin V Mar 27 '13 at 08:21