user475685
user475685

Reputation: 8319

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>

Upvotes: 15

Views: 217580

Answers (5)

Roko C. Buljan
Roko C. Buljan

Reputation: 206048

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

Upvotes: 2

nonozor
nonozor

Reputation: 934

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

Upvotes: 5

dfortun
dfortun

Reputation: 764

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);

Upvotes: -2

Faraona
Faraona

Reputation: 1700

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

Upvotes: 8

Joey
Joey

Reputation: 354436

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

Upvotes: 3

Related Questions