How to Write to a Text File in JavaScript

A JavaScript function that fire on the button click event. I’ll show you how to do that in a couple minutes! To create a text file from javascript, we’ll need to use Blob object.

Example

function WriteToFile(passForm) {

set fname = CreateObject("Scripting.FileSystemObject");
set solution = fname.CreateTextFile("<your Path>/pakainfo.txt", True);

var MemberUserName = document.getElementById('MemberUserName');
var memberSirname = document.getElementById('memberSirname');

solution.writeline("Member User Name :" + MemberUserName);
solution.writeline("Member Sir Name :" + memberSirname);

solution.writeline("-----------------------------");
solution.Close();
}

// HTML Source Code
<form onSubmit="WriteToFile(this)">
<label>Type your Member name:</label>
<input type="text" name="MemberUserName" id="MemberUserName" size="20">

<label>Type your Member sir name: </abel>
<input type="text" name="memberSirname" id="memberSirname" size="20">

<input type="submit" value="submit">
</form>

javascript write to file

first of all Requiring fs module in which writeFile function is defined. and Data which will write in a file and // Write data in ‘Results.txt’.

<script>

const fs = require('fs')

let data = "Welcome To Pakainfo.com free download source code."
fs.writeFile('Results.txt', data, (err) => {
// In case of a error throw err.
if (err) throw err;
})
</script>

write to file

var mainExFlData = "c:/product.txt";
var flName = new File(mainExFlData);
var userString = "Welcome To Pakainfo.com";

flName.open("w"); // open file with write access
flName.writeln("First line of text");
flName.writeln("Second line of text " + userString);
flName.write(userString);
flName.close();

read from file

var mainExFlData = "c:/product.txt"
var flName = new File(mainExFlData);

flName.open("r"); // open file with read access
var userString = "";
while (!flName.eof) {
// read each line of text
userString += flName.readln() + "\n";
}
flName.close();
alert(userString);

writeTextFile write data to file on hard drive

function writeTextFile(filepath, output) {
var mainExFlData = new File(filepath);
mainExFlData.open("w"); //
mainExFlData.writeln(output);
mainExFlData.close();
}

readTextFile read data from file

function readTextFile(filepath) {
var userString = "";
var mainExFlData = new File(filepath);
mainExFlData.open("r");
while (!mainExFlData.eof) {
// read each line of text
userString += mainExFlData.readln() + "\n";
}
return userString;
}

JavaScript write to text file

<html>
<h2>Create Text file in JavaScript</h2>
<script>
function createFile(){
var object = new ActiveXObject("Scripting.FileSystemObject");
var file = object.CreateTextFile("C:\\Welcome.txt", false);
file.WriteLine('Welcome To Pakainfo');
file.WriteLine('Free Download Source code for Pakainfo,com');
file.Close();
}
</script>
<input type="Button" value="Make File" onClick='createFile()'>
</html>

read-write-file.js

var mainExFlData = "c:/product.txt";
var flName = new File(mainExFlData);
var product_desc = "My string of text";

flName.open("w");
flName.writeln("First line of text");
flName.writeln("Second line of text " + product_desc);
flName.write(product_desc);
flName.close();

var mainExFlData = "c:/product.txt"
var flName = new File(mainExFlData);

flName.open("r"); // open file with read access
var product_desc = "";
while (!flName.eof) {
product_desc += flName.readln() + "\n";
}
flName.close();
alert(product_desc);

function writeTextFile(filepath, output) {
var mainExFlData = new File(filepath);
mainExFlData.open("w"); //
mainExFlData.writeln(output);
mainExFlData.close();
}

function readTextFile(filepath) {
var product_desc = "";
var mainExFlData = new File(filepath);
mainExFlData.open("r");
while (!mainExFlData.eof) {
// read each line of text
product_desc += mainExFlData.readln() + "\n";
}
return product_desc;
}

JavaScript Read and Write to Text File

readFile(Path, Options, Callback)

writeFile:

writeFile(Path, Data, Callback)

I hope you get an idea about javascript write to text file.


#javascript 

How to Write to a Text File in JavaScript
1.00 GEEK