Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a text file at C:\Test.txt. the value inside it is 100.

Now i have to open this file and read the value in the file for an interval of evry 2 secs. My questions are:

1.) Can i do this using standard javascript functions without using using php and activex? If so what is the function?

2.) is fopen, fread and fwrite javscript functions? as specified here
"http://www.careerride.com/JScript-read-and-write-file.aspx[^]"

3.) when i do this "var fh = fopen("c:\\Test.txt", 0);" i am getting error fopen is not defined.
Posted
Updated 6-Jun-12 9:11am
v2

I believe fopen is a Java function not Javascript.

For very good security reasons you can't access the file without the user first selecting it via an file input control. With newer browsers there is support for dragging and dropping files, the browser treats the file input control as a native drop target.

The following article explains how this can be achieved with Javascript using the file input control.

Reading local files in JavaScript[^]
 
Share this answer
 
Comments
amarasat 7-Jun-12 10:54am    
You are right, i started implementing it using synchronous "xmlHttpRequest" to call a php function to read the text file and return the value back.

Its working fine, but when php function returns the value it returns it as
request.responsetext = "100" instead of 100

How do i remove the quotes from the response text?
Stephen Hewison 7-Jun-12 17:03pm    
The quotes wont be added by javascript. Either your php code is doing it or it's in the file. But that said, all response text values are strings. If it's always going to be a number you can convert it to an int using parseInt(value, 10).
amarasat 8-Jun-12 14:29pm    
I was able to remove the quotes in 2 ways,

1.)y = "100.12";
y = +y; //removes the quotes and gives 100.12

or

2.) y = "100.12";
y = parseInt(y, 10); //also removes the quotes and gives 100.


Now how do i do this:

3.) y = "[100.01, 101.23]";
now using above 2 ways, it gives (y = NaN)
how do i get y = [100.01, 101.23];
amarasat 8-Jun-12 14:44pm    
got it thanks a lot for your help!! i have to use
"JSON.parse(Request.responseText);"
C#
var fso, ts,s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.OpenTextFile("c:\\testfile.txt", 1);
s = ts.ReadLine();
 
Share this answer
 
Comments
amarasat 7-Jun-12 10:51am    
I am getting an error ActiveXObject not defined, do i have to include any packages or libraries etc?
nate.good 22-Aug-13 13:47pm    
throwable's solution, didn't work for me but with altering it a little I got it to work using the code below.

var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile("C:\\output.txt", 1, false, 0);
var output = fh.ReadAll();

hope it helps!
Tim Smith AWW 16-Aug-16 5:13am    
I also found that slight bug with the variable. Thanks for your fix - got it working for me.
Also the ReadAll() update was very helpful!
Member 11441994 11-Dec-17 7:12am    
I guess ActiveX objects are only supported by IE and not by Chrome and Firefox. So in such cases what can be used?
Hi . This is your response :
JavaScript
<html> 
  <head> 
    <title>Ajax at work</title> 

    <script language="javascript">
      var XMLHttpRequestObject = false; 

      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
      }

      function getData(dataSource, divID) 
      { 
	  
        if(XMLHttpRequestObject) {
          var obj = document.getElementById(divID); 
          XMLHttpRequestObject.open("GET", dataSource); 

          XMLHttpRequestObject.onreadystatechange = function() 
          { 
            if (XMLHttpRequestObject.readyState == 4 && 
              XMLHttpRequestObject.status == 200) { 
                obj.innerHTML = XMLHttpRequestObject.responseText; 
            } 
          } 

          XMLHttpRequestObject.send(null); 
        }
      }
    </script>
  </head> 

  <body>

    <h1>Fetching data with Ajax & javaScript</h1>

    <form>
      <input type="button" value="Display Message">
        onclick = "getData('data.txt', 'targetDiv')"> 
    </input></form>

    <div id="targetDiv">
      <p>The fetched data will go here.</p> 
    </div> 

  </body> 
</html>

Then create a data.txt near your html file.
Notice: If you work with windows you should run your project from IIS but if work with Linux or ubuntu you should run your project from terminal :
1-at terminal write "sudo nautilus".
2-then enter your password
3-then open a window and go to this address /var/www .
4-and copy your project in this file.
5-then open FireFox browser then enter this address in toolbar http://localhost
for example :http://localhost/XMLHttpRequest1/xmlhttprequest1.htm
now you can see your data .
 
Share this answer
 
v3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900