By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,149 Members | 619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,149 IT Pros & Developers. It's quick & easy.

How to read text file using JS without activex control?

P: 54
I am trying to read text file , here is the code ,

I cant use activex control so plz give me some way....
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  5. function r()
  6. {
  7.     alert("Hkhi");
  8. var fh = fopen("G:\a.txt",0); // Open the file for writing
  10. //var fh = fopen("C://Documents and Settings//Administrator//Desktop//log.txt",0);
  11. alert(fh);
  12. if(fh!=3) // If the file has been successfully opened
  13. {
  14.     alert("Hi");
  15.     var str = "Some text goes here...";
  16.     fread(fh, str); // Write the string to a file
  17.     fclose(fh); // Close the file 
  18. alert("Hi");
  19. }
  22. </script>
  23. </head>
  24. <body onload="r()">
  25. </body>
  26. </html>
Apr 4 '11 #1
Share this Question
Share on Google+
8 Replies

Expert Mod 5K+
P: 8,639
you can’t do that. JavaScript is not allowed to access the client’s file system (ActiveX being the exception, obviously)

besides that, fopen(), fread() & fclose() are typical PHP functions (not sure about ActiveX, but it doesn’t seems like they belong there, too)
Apr 4 '11 #2

P: 54
so you mean that its not possible to read file using js?

my problem is that I have to generate graph in html page using the parameter coming from one sensor , which are being stored in text file...

Is it possible to store this parameter somewhere and then accessed in js file? because I want that parameters in my file which is generating the graph..
Apr 4 '11 #3

Expert Mod 5K+
P: 8,639
you can only access files from the server you serve your page from (cf. SOP (Same Origin Policy))
Apr 4 '11 #4

P: 54
ok ,

So is it possible that I store parameters from local file using anyother language on webpage and the using JS access those parameters from that webpage?
Apr 4 '11 #5

Expert Mod 5K+
P: 8,639
I donít know any language that could access the local filesystem (besides JScript).
Apr 4 '11 #6

P: 54
thank for your help and reply ,

Now I just need to access the file on server ,
Can you please give me way to read our values from the file lying on the same folder on server...
Apr 4 '11 #7

Expert Mod 5K+
P: 8,639
thatís simple, just do an AJAX request of the file.
Apr 4 '11 #8

P: 54
thanks but I dont know too much about how to use AJAX , can you please just show me demo code for that....
Apr 4 '11 #9

Post your reply

Sign in to post your reply or Sign up for a free account.