-2

I want to know how to write strings in a textfile on my machine when I click on a button in my Html page using Javascript... I want an Internet Explorer, Chrome and Firefox compatibility...

Thanks

5
  • 1
    Just imagine what hackers could do if JavaScript had access to your filesystem... Commented Jul 4, 2016 at 18:30
  • stackoverflow.com/questions/16055391/… Commented Jul 4, 2016 at 18:33
  • It seems from a comment that you're using ActiveX. Good luck persuading Mozilla and Google to support it. Commented Jul 4, 2016 at 18:36
  • developers.google.com/web/updates/2011/08/… Commented Jul 4, 2016 at 18:36
  • what you could do, is to create a virtual file with JavaScript, which would be downloadable like any other file on a remote server Commented Jul 4, 2016 at 18:48

2 Answers 2

0

It's not possible. It would be a major security risk if browsers could write anything on the user's filesystem.

Best you could do is create a text file on your server, and serve it for download.

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

2 Comments

if I use that code, it's working, but the file goes directly on the desktop... If I change the filename to C:\test\testfile.txt, nothing happend... var ForReading = 1, ForWriting = 2, ForAppending = 8; var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0; var fso = new ActiveXObject("Scripting.FileSystemObject"); var filename = "testfile.txt"; fso.CreateTextFile(filename); var fileObj = fso.GetFile(filename); var ts = fileObj.OpenAsTextStream(ForWriting, TristateUseDefault); ts.WriteLine("Hello World!"); ts.WriteLine("The quick brown fox"); ts.Close();
@ThomasJomphe It shouldn't even be possible to write to the client's desktop. That must either be a vulnerability, or the browser realizing it's being served locally. You cannot write directly to a client's drive. The best you can do is make use of LocalStorage.
-3

There ara some questions like yours here in stackoverflow, try some these links

Is it possible to write data to file using only JavaScript?

How to read and write into file using JavaScript

or you can try this gist example

Edit:

The Gist example only works for node.js sorry

1 Comment

Are you read comments of your gist?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.