Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
1.12/5 (3 votes)
See more:
Pls tell me how to read a text file in javascript and i want to split that text by delimiter space..
Posted
Updated 10-Oct-18 6:10am

Pretty much, you can't - javascript very deliberately does not have file manipulation functions for security reasons. Imagine if you could: a rogue website could access your HDD and read (or write) and file it wanted to...

If you have a very, very good reason for this, it may be possible under exceptional circumstances and with the clients active permission, but this is not going to occur in the vast majority of cases (and any site which asked me would get told "No" and never visited again).

Why do you want to do that?
 
Share this answer
 
 
Share this answer
 
Comments
wots up frnds 10-Jul-13 3:22am    
for me this is enough wishing all the best bharth
If you want to read a file stored at server side, use Ajax to read it.
xmlHttp.open("GET","file.txt",true);

If you want to read a file stored in local computer, there isn't a direct way to do this. But if your local computer is a Windows computer, you can use the FileSystemObject. Take a look at here[^].
 
Share this answer
 
Comments
Bharath Kumar Gorle 10-Jul-13 3:07am    
By using FileReader we can read the file..But i am not getting the exact result..could you pls help me...
thanh_bkhn 10-Jul-13 3:30am    
So how did you code? You should use "Improve question" to post your code here and we may find something wrong
JavaScript
function readSingleFile(evt) {
    //Retrieve the first (and only!) File from the FileList object
    var f = evt.target.files[0]; 
  if(f){
    var r = new FileReader();
      r.onload = function(e) { 
	   var contents = e.target.result;
       }
    }
}
 
Share this answer
 
my solution to this was to put data into a .JS file as a variable (using backend s/w - like VBA/Excel).
And also in other longstanding instances, use existing .HTM files (4 in succession).

Then process the variable (or .innerHTM's) and re-load the results into the IFRAME via .innerHTM

this works over the internet on all browsers I can test with (including Android OS) for files on the same domain. However there are issues for a system using local files (to the PC) with Chrome, & presumably Chrome based browsers like Opera. FireFox and IE seem to handle locally.

In a more recent project which was to reside on an Android phone, it was found that writing back to the IFRAME didn't work, and the reason was never determined. The post processing of the data was written to a
layer (.innerHTM). The IFRAME was re-loaded many times from a suite of .htm files.
Only FireFox on Android (from local files) worked. Chrome did not handle it.

An attempt was made to load .txt files into an IFRAME which never seemed to work fully locally, even in FireFox.
 
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