How to read a local text file?

前端 未结 20 2263
北恋
北恋 2020-11-21 05:28

I’m trying to write a simple text file reader by creating a function that takes in the file’s path and converts each line of text into a char array, but it’s not working.

20条回答
  •  离开以前
    2020-11-21 05:47

    In order to read a local file text through JavaScript using chrome, the chrome browser should run with the argument --allow-file-access-from-files to allow JavaScript to access local file, then you can read it using XmlHttpRequest like the following:

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
       if (xmlhttp.readyState == 4) {
           var allText = xmlhttp.responseText;          
                }
            };
    xmlhttp.open("GET", file, false);
    xmlhttp.send(null);
    

提交回复
热议问题