Showing posts with label write file javascript. Show all posts
Showing posts with label write file javascript. Show all posts

Sunday, February 21, 2010

How to Write and Read Some Content into (or from) File Using Java Script

To write some content into some file or read content from file and wants to store all file content in one variable and wants to dispaly this as a alert or some thing the following program will be helpful.This works in Internet Explorer.
Code:
<HTML>

<HEAD>


<SCRIPT language="JavaScript">


function WriteAndReadFile()

{

var fso = new ActiveXObject("Scripting.FileSystemObject");

var fh = fso.CreateTextFile("D:\\Test.txt", true);

fh.WriteLine("Text Example 1");

fh.WriteLine("Text Example 2");

fh.WriteLine("Text Example 3");

var f;

var str;

f = fso.OpenTextFile("D:\\Test.txt", 1);

var s=f.ReadAll();

alert(s);

f.Close();


}


</SCRIPT>

</HEAD>


<BODY>

<P>

<SCRIPT language="JavaScript"> WriteAndReadFile(); </SCRIPT>

</P>

</BODY>

</HTML>