user3265409
user3265409

Reputation: 3

Create file using javascript code not working

This code is not working in any Browser

function Submit1_onclick() {
    var fso, f1, ts;
    var ForWriting = 2;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    fso.CreateTextFile("F:/test1.txt");
    f1 = fso.GetFile("F:/test1.txt");
    ts = f1.OpenAsTextStream(ForWriting, true);
    // Write a line with a newline character.
    tf.WriteLine("Testing 1, 2, 3.");
    // Write three newline characters to the file.
    tf.WriteBlankLines(3);
    // Write a line.
    tf.Write("This is a test.");
    tf.Close();
}

Upvotes: 0

Views: 833

Answers (2)

GitaarLAB
GitaarLAB

Reputation: 14655

user2965026 already gave the correct answer (typo).

But the asker also has another problem as stated in his comments: Error: Automation server can't create object

If you google this question you'll find what I already stated in my comment to your question: you need to elevate the security rights of the script:

  • Go to inernet options.
  • Select security tab.
  • Under Custom level.
  • Ensure that "Initialize and script active x controls is not marked safe for scripting" is enabled and try to run your code now

Better is to add your site to the trusted domains (and check this option in that section), instead of opening this security hole for all websites!

Upvotes: 1

xiawenqi
xiawenqi

Reputation: 11

// Write a line with a newline character.
tf.WriteLine("Testing 1, 2, 3.");
// Write three newline characters to the file.
tf.WriteBlankLines(3);
// Write a line.
tf.Write("This is a test.");
tf.Close();

Change the tf to ts, you had a typo. And run it in IE. I tried it and it works in IE9. You can see your typos in console (Press F12).

Upvotes: 1

Related Questions