Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a text file using javascript

I am using the following code to create a text file using javascript and it's not working

<html>
    <head>
        <script language="javascript">
            function WriteToFile()
            { 
                var txt = new ActiveXObject("Scripting.FileSystemObject");
                var s = txt.CreateTextFile("11.txt", true);
                s.WriteLine('Hello');
                s.Close();
             }
         </script>
    </head>
   <body onLoad="WriteToFile()">
   </body>
</html>
like image 460
user475685 Avatar asked Mar 23 '11 10:03

user475685


People also ask

How to write text to a file in JavaScript?

It is basically a JavaScript program (fs.js) where function for writing operations is written. Import fs-module in the program and use functions to write text to files in the system. The following function will create a new file with a given name if there isn’t one, else it will rewrite the file erasing all the previous data in it.

How to create a text file from JavaScript blob?

To create a text file from javascript, we’ll need to use Blob object. Blob (A Binary Large OBject) is a collection of binary data stored as a single entity. So, we’re going to create a Blob object that contains our text content. Then we’ll convert a blob into a text file which web browser will then popup the download dialog box for the users.

How to create and save the data into a text file?

In this tutorial, we will create and save the data into a text file. To do that we’ll write: A JavaScript function that fire on the button click event. Create a Blob constructor, pass the data in it to be to save and mention the type of data. And finally, call the saveAs (Blob object, "your-file-name.text") function of FileSaver.js library.

How to create and save files in JavaScript?

The possible ways to create and save files in Javascript are: Use a library called FileSaver – saveAs (new File ( ["CONTENT"], "demo.txt", {type: "text/plain;charset=utf-8"}));


1 Answers

Try this:

<SCRIPT LANGUAGE="JavaScript">
 function WriteToFile(passForm) {

    set fso = CreateObject("Scripting.FileSystemObject");  
    set s = fso.CreateTextFile("C:\test.txt", True);
    s.writeline("HI");
    s.writeline("Bye");
    s.writeline("-----------------------------");
    s.Close();
 }
  </SCRIPT>

</head>

<body>
<p>To sign up for the Excel workshop please fill out the form below:
</p>
<form onSubmit="WriteToFile(this)">
Type your first name:
<input type="text" name="FirstName" size="20">
<br>Type your last name:
<input type="text" name="LastName" size="20">
<br>
<input type="submit" value="submit">
</form> 

This will work only on IE

like image 75
Faraona Avatar answered Oct 13 '22 01:10

Faraona