问题
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