سؤال

I want to save something in txt file by using JavaScipt

<input type="date" id="date1">
  <script>
  var a=document.getElementByid("date1");
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var fh = fso.CreateTextFile("Test.txt", 8, true);
    fh.WriteLine("Something");
    fh.Close();

  </script>

I tried this but it's not working. My main idea is to get value form input and save it in Test.txt

لا يوجد حل صحيح

نصائح أخرى

You can't; not reliably, cross-browser. JavaScript in the browser (where you're clearly using it) runs in a sandbox and is not allowed to access the local filesystem. Some older versions of IE will allow you to use the FileSystemObject in the way shown in your question, but only with masses of security warnings, and modern versions don't allow it at all.

The File API and FileSystem API allow limited reading from files in the file system, but writing is still a work in progress.

Your best bet is to post a form to your server, probably with target="_blank", and have the server respond with a Content-Disposition: attachment; filename=Test.txt header and the file data, which will cause the browser to ask the user where to save it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top