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.
Asked
Active
Viewed 262 times
0
-
.... like write to an external file? Or the same file? – LazerSharks Mar 27 '13 at 07:24
-
Yes that is exactly what i mean Gnuey, an Externeal file. – LittleProgrammer Mar 27 '13 at 07:25
-
To a local file on the user's computer or a file on the server? – JJJ Mar 27 '13 at 07:27
-
A file on the server. – LittleProgrammer Mar 27 '13 at 07:30
-
But are you using some form of JavaScript server-side? – Ale Mar 27 '13 at 07:37
-
[This could help you out. Digged out on SO.](http://stackoverflow.com/questions/14795357/javascript-in-html-write-to-file) – Popoola Oluwaseun Kirchoff Mar 27 '13 at 07:41
-
No i am not useing JavaScript server-side... I am not quite sure how to use it server-side :( – LittleProgrammer Mar 27 '13 at 07:46
1 Answers
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
-
1No 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