javascript write to text file – how to write text to text file in javascript?

javascript write to text file – i am going to create a file and write text into it using JavaScript. Use the blob.text() Function to Read Text Files in JavaScript.

javascript write to text file

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("/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

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’.

 

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



Create Text file in JavaScript

Don’t Miss : Load External HTML File With JQuery

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.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment