php - How to write data to textfile using JavaScript - Stack Overflow

I have some problem to write date to a file Test.txt by using JavaScript. I have find answer in good to

I have some problem to write date to a file Test.txt by using JavaScript. I have find answer in good too but I am still can't solve it. This is my code

<script type="text/javascript">
function WriteFile(){
       var fso  = new ActiveXObject("Scripting.FileSystemObject");
       var fh = fso.CreateTextFile("Test.txt", 8,true);
       x=document.getElementById("name").value;
       fh.WriteLine(x);
       fh.Close();
}

And

<form>
    <input type="text" id="name"/>
    <input type="button" value="Save" id="write" onclick="WriteFile()"/>
</form>

I think it's should be run well by I the simple example I see from ebook.

I am not sure with path of Test.txt. I put it in the same forder in my localhost.

What am I doing wrong?

I have some problem to write date to a file Test.txt by using JavaScript. I have find answer in good too but I am still can't solve it. This is my code

<script type="text/javascript">
function WriteFile(){
       var fso  = new ActiveXObject("Scripting.FileSystemObject");
       var fh = fso.CreateTextFile("Test.txt", 8,true);
       x=document.getElementById("name").value;
       fh.WriteLine(x);
       fh.Close();
}

And

<form>
    <input type="text" id="name"/>
    <input type="button" value="Save" id="write" onclick="WriteFile()"/>
</form>

I think it's should be run well by I the simple example I see from ebook.

I am not sure with path of Test.txt. I put it in the same forder in my localhost.

What am I doing wrong?

Share Improve this question edited Mar 6, 2020 at 20:19 user315139192111315 234 bronze badges asked Mar 7, 2013 at 3:06 sk juli kakask juli kaka 4701 gold badge5 silver badges20 bronze badges 2
  • What's the 8 for? Just use var fh = fso.CreateTextFile("c:\\Test.txt",true); – danleyb2 Commented Jul 11, 2015 at 9:22
  • @danleyb2 , var FileObject = fso.CreateTextFile("c:\\Test.txt", 8, true); // 8=append, true=create if not exist – sk juli kaka Commented Aug 24, 2015 at 5:04
Add a ment  | 

2 Answers 2

Reset to default 1

This the example you could go through:-

function WriteToFile(passForm) {
 
    set fso = CreateObject("Scripting.FileSystemObject"); 
    set s   = fso.CreateTextFile("<your Path>/filename.txt", True);
 
    var firstName = document.getElementById('FirstName');
    var lastName  = document.getElementById('lastName');
 
    s.writeline("First Name :" + FirstName);
    s.writeline("Last Name :" + lastName);
 
    s.writeline("-----------------------------");
    s.Close();
 }
<form onSubmit="WriteToFile(this)">
<label>Type your first name:</label>
<input type="text" name="FirstName" id="firstName" size="20">
 
<label>Type your last name: </abel>
<input type="text" name="LastName" id="lastName" size="20">
 
<input type="submit" value="submit">
</form>

According to http://msdn.microsoft./en-us/library/czxefwt8(v=vs.84).aspx it looks like you need to specify the full path for your file.

E.g. "C:\\Test.txt"

Update:

Just tested this in IE9 and it works.

<head>
  <script>
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var fh = fso.CreateTextFile("c:\\Test.txt", 8, true);
    fh.WriteLine("foo");
    fh.Close();
  </script>
</head>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742419154a4440334.html

相关推荐

  • php - How to write data to textfile using JavaScript - Stack Overflow

    I have some problem to write date to a file Test.txt by using JavaScript. I have find answer in good to

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信