Take the 2-minute tour ×

I am trying to read a text file using javascript and display the content in html file. Its working as i have created the browse button to select the text file but i want to read the content from a fixed path file .In this program instead of taking the file path i want to read file from the path like - D:/new folder/abc.text

I am using the following code.

 <script type="text/javascript">
 function readSingleFile(evt) {
 var f = evt.target.files[0]; 
 if (f) {
 var r = new FileReader();
 r.onload = function(e) { 
 var contents = e.target.result;
 document.write("the contents of the file are<br>");
  } else { 
   alert("Failed to load file");
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

...but i want to read the content from a fixed path

You can't, not on a web browser, using standards, and accessing the local file system. There's a huge difference between allowing the browser to read a file the user has specifically identified for the page, and allowing it to read any file it wants. You simply cannot do the latter without resorting mechanisms (which will trigger security stuff) such as ActiveX, Flash, signed Java applets, and the like. The File API requires a file input element as a starting point for a reason.

share|improve this answer
wow what an explanation, +1 –  Hanky 웃 Panky Dec 31 '12 at 6:03
The script definitly will execute after allow from the user then what is the security concern here i just need to read file from fixed path nothing else i need. –  Adesh singh Dec 31 '12 at 6:57
@user1868052: As I said, I don't believe there's any way to do that with the File API as it currently stands. A signed Java applet can access the local file system (with the user's agreement), a Silverlight control with adequate runtime permissions (again per the user), but the starting point in the File API is an input[type=file]. Mind you, they've updated the specification since the last time I had a really good look. But glancing through just now, it still looks like it relies on that input. –  T.J. Crowder Dec 31 '12 at 7:03
is it not possible with the help of ActiveXObject. is there any possibility such that i could fixed the path for the file in htmlcode –  Adesh singh Dec 31 '12 at 7:40
@user1868052: Again: No. I thought I was clear about that, in the answer and then again in the comment above. (And it is possible with ActiveX, on IE, but obviously that's just IE.) –  T.J. Crowder Dec 31 '12 at 8:20
add comment

Your Answer


