-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

vp_arth
  • 14,461
  • 4
  • 37
  • 66
Thomas Jomphe
  • 117
  • 6
  • 17

2 Answers2

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.

Schlaus
  • 18,144
  • 10
  • 36
  • 64
  • 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(); – Thomas Jomphe Jul 04 '16 at 18:33
  • @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`. – Carcigenicate Jul 04 '16 at 18:43
-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

Community
  • 1
  • 1