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>
4
  • 1
    Hit F12. Look at console. Rethink your approach Commented Nov 17, 2016 at 8:57
  • I didn't knew javascript has fopen function ? Commented Nov 17, 2016 at 8:58
  • javascript is client side . fwrite(),fclose() all are php .webdeveloper.com/forum/… Commented Nov 17, 2016 at 8: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. Commented Nov 17, 2016 at 9:00

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

where did you call the "txt" variable here?
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.