Reading a txt file from Javascript

前端 未结 3 1942
猫巷女王i
猫巷女王i 2020-12-07 05:52

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.FileS         


        
相关标签:
3条回答
  • 2020-12-07 06:04

    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.

    0 讨论(0)
  • 2020-12-07 06:07

    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.

    0 讨论(0)
  • 2020-12-07 06:22

    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.

    0 讨论(0)
提交回复
热议问题