Reading a txt file from Javascript

霸气de小男生 提交于 2020-01-19 06:22:47

问题


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?!


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/5135610/reading-a-txt-file-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!