简体   繁体   中英

Reading a txt file from Javascript

am trying to read few lines from a txt file using JS,and i have this code but its not working for some reason,,

var fso = new ActiveXObject("Scripting.FileSystemObject"); 

var s = fso.OpenTextFile("C:\\wamp\\www\\22.txt", 1, true);

var row = s.ReadLine();


alert(row);

any suggestions?!

Make sure your browser has the right permissions to perform that kind of operation. Usually, browsers won't allow direct file system access by default.

Only IE supports ActiveXObject . Trying to use ActiveXObject on any other browser will fail because there is no such variable defined.

You need to either limit yourself to IE, write a browser plugin instead, or give up trying to get file system access on other browsers and proxy files through a server instead.

If you're running WAMP anyway, just use standard AJAX to fetch the file 22.txt from the server. The easiest way is to use jQuery, where the code would be:

$.get("22.txt", function(data) {
    alert(data);
}

You can search for how to do this without jQuery if you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM