javascript how to read a local file Denied access

强颜欢笑 提交于 2020-01-07 05:28:04

问题


I want to read a local file with javascript. I have the following function

$(function() {
        console.log("antes de readTextFile");
        readTextFile("file:///D:/carlota/eusruveyadmin/manuales/ficheropprueba.txt")

    });

    function readTextFile(file)
    {
        console.log("readFile principio");
        var rawFile = new XMLHttpRequest();
        rawFile.open("GET", file, false);
        rawFile.onreadystatechange = function ()
        {
            if(rawFile.readyState === 4)
            {
                if(rawFile.status === 200 || rawFile.status == 0)
                {
                    var allText = rawFile.responseText;
                    console.log("readFile fichero ",allText);
                }
            }
        }
        rawFile.send(null);
    }

In the console browser I have the following message "denied access"

I checked in the browser my file and I can see the file

How do I have to write the path of file?


回答1:


If you're using IE you need to set Settings/Tools->Internet Options->Security->Custom Level and change security settings under "Miscellaneous" set "Access data sources across domains" to Enable.

But it won't work for the users of your app, if they're using IE and don't have the same setting



来源:https://stackoverflow.com/questions/42829082/javascript-how-to-read-a-local-file-denied-access

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