16

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>
2
  • I think you need to install ActiveX plugin, which browser you use? Commented Mar 23, 2011 at 10:46
  • 5
    ActiveX is for Microsoft browsers only, you should NOT be using that if you want cross browser compatibility. As for writing to a file, this is not allowed for a good reason. Consider how many times your hard-drive would have been erased while surfing random internet sites, if this was allowed. Commented Mar 23, 2011 at 10:51

5 Answers 5

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

You need to install ActiveX plugin
i am using a mac system and i just replaced set s = fso.CreateTextFile("C:\test.txt", True); by set s = fso.CreateTextFile("file/test.txt", True); and the file is not getting created
I think this is not possible to do with java script on safari and firefox. You must find other solution.
5

That works better with this :

var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();

http://msdn.microsoft.com/en-us/library/5t9b5c0c(v=vs.84).aspx

Comments

3

From a web page this cannot work since IE restricts the use of that object.

Comments

3
<textarea id="content" placeholder="Content..." cols="32" rows="7">Lorem ipsum
dolor sit amet!</textarea><br>
<input type="text" id="fileName" placeholder="download.txt">
<button type="button" id="download">Download</button>

<script>
// DOM utility functions:

const el = (sel, par) => (par || document).querySelector(sel);
const elNew = (tag, prop) => Object.assign(document.createElement(tag), prop);


// Create file and download it:

const createAndDownload = (content, download = "download.txt", type = "text/plain") => {
  const file = new Blob([content], { type });
  const href = URL.createObjectURL(file);
  const elAnchor = elNew("a", { href, download });
  el("body").append(elAnchor);
  elAnchor.click();
  elAnchor.remove();
  URL.revokeObjectURL(href);
};


// Usage example:

el("#download").addEventListener("click", () => {
  const text = el("#content").value;
  const name = el("#fileName").value;
  createAndDownload(text, name, "text/plain");
});
</script>

Comments

-2

You have to specify the folder where you are saving it and it has to exist, in other case it will throw an error.

var s = txt.CreateTextFile("c:\\11.txt", true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.