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.
-
.... like write to an external file? Or the same file?LazerSharks– LazerSharks2013-03-27 07:24:32 +00:00Commented Mar 27, 2013 at 7:24
-
Yes that is exactly what i mean Gnuey, an Externeal file.LittleProgrammer– LittleProgrammer2013-03-27 07:25:37 +00:00Commented Mar 27, 2013 at 7:25
-
To a local file on the user's computer or a file on the server?JJJ– JJJ2013-03-27 07:27:47 +00:00Commented Mar 27, 2013 at 7:27
-
A file on the server.LittleProgrammer– LittleProgrammer2013-03-27 07:30:55 +00:00Commented Mar 27, 2013 at 7:30
-
But are you using some form of JavaScript server-side?Ale– Ale2013-03-27 07:37:25 +00:00Commented Mar 27, 2013 at 7:37
|
Show 2 more comments
1 Answer
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);
4 Comments
LittleProgrammer
Sorry, It isn't working. Thank you for trying to help though :)
Peter Herdenborg
@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
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.
Valentin V
yes, this is a basic piece of code, which I took on the Internet, but it is easily adaptable.