Solved

how to read text file using javascript

Posted on 2006-07-02
4
16,205 Views
Last Modified: 2011-10-03
how do i read a text file using javascript activexobject
on local machine.
0
Comment
Question by:jiniasp
4 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 25 total points
ID: 17027598
Only for IE - FireFox won't allow ActiveX:

function readit(fileName) {
   var data = "";
   var fileObj = new ActiveXObject("Scripting.FileSystemObject");
   if (fileObj.FileExists(fileName)) {
      var txtStream = fileObj.OpenTextFile(fileName,1);
      data = txtStream.ReadAll();
      txtStream.Close();
   }
   return data;
}
0
 
LVL 25

Assisted Solution

by:clockwatcher
clockwatcher earned 25 total points
ID: 17027780
Here's a cross-browser solution, but kind of pointless... if you're trying to serve up this webpage from an external website and expecting to be able to read a local file, security restrictions will prevent it - no matter what way you go.

c:\example.htm
---------------
<script language="javascript">
      
if (window.XMLHttpRequest) {    
      req = new XMLHttpRequest();    
}    
else if (window.ActiveXObject) {    

      req = new ActiveXObject("Microsoft.XMLHTTP");    
}    

req.open('GET', "file:///C:/test.txt");

req.onreadystatechange = function() {    
        if (req.readyState == 4) {    
      alert(req.responseText);
} }
req.send("");

</script>
   
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now