0

I am learning javascript and so I was trying to append a string to a text file using Javascript. I tried the following but for some reason nothing happens. Where could I be doing wrong ? I am running the code on Mozilla firefox browser.

<%-- 
    Document   : demo
    Created on : 17 Nov, 2016, 11:01:01 AM
    Author     : user
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>

          <button onclick="WriteFile()">Click me</button>  
            <p id="demo"></p>


<script type="text/javascript">
    function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
   function WriteFile()
{

    var fh = fopen("C:\\javascriptlogs\\myfile.txt", 3); // Open the file for writing

    if(fh!=-1) // If the file has been successfully opened
    {
        var str = "Some text goes here...";
        fwrite(fh, str); // Write the string to a file
        fclose(fh); // Close the file
    }

}


    </script>

     </body>
</html>
Farheen
  • 219
  • 2
  • 3
  • 12
  • 1
    Hit F12. Look at console. [Rethink your approach](http://stackoverflow.com/questions/19744155/writing-file-to-desktop-using-html5-filesystem-api) – mplungjan Nov 17 '16 at 08:57
  • I didn't knew javascript has `fopen` function ? – abhishekkannojia Nov 17 '16 at 08:58
  • javascript is client side . `fwrite(),fclose()` all are php .http://www.webdeveloper.com/forum/showthread.php?210061-how-to-save-a-file-using-javascript – prasanth Nov 17 '16 at 08:59
  • As far as I know, you won't be able to do this normally (due to security concerns in Javascript writing files on any computers, let those be your server, or the client). The only way I've seen Javascript to write directly to a file on your server is running your application on a node server. – Alex Szabo Nov 17 '16 at 09:00

2 Answers2

2

Unfortuanlly, JavaScript doesn't have a fopen function like known from other programming languages. Javascript cannot typically access local files in new browsers but the XMLHttpRequest object can be used to read files. So it is actually Ajax (and not Javascript) which is reading the file.

This is a little example that may get you started.

var txt = '';
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
  if(xmlhttp.status == 200 && xmlhttp.readyState == 4){
    txt = xmlhttp.responseText;
  }
};
xmlhttp.open("GET","abc.txt",true);
xmlhttp.send();
Anna Jeanine
  • 3,975
  • 10
  • 40
  • 74
2

fopen, fwrite, fclose, are not defined browser JavaScript functions, and browser JavaScript does not have direct access to the local computer's file system by design. If it did, any website could read files from your computer -- that would be pretty worrisome as most people have personal stuff stored there!

You could use node.js to do this, but wouldn't run in the browser.

Joe
  • 2,500
  • 1
  • 14
  • 12