javascript如何读取本地文件拒绝访问 [英] javascript how to read a local file Denied access

查看:56
本文介绍了javascript如何读取本地文件拒绝访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 javascript 读取本地文件.我有以下功能

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

文件路径怎么写?

推荐答案

如果您使用的是 IE,则需要设置 Settings/Tools->Internet Options->Security->Custom Level 并更改Miscellaneous"设置下的安全设置启用跨域访问数据源".

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.

但如果您的应用用户使用的是 IE 并且没有相同的设置,则它不会对您的应用程序用户起作用

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

这篇关于javascript如何读取本地文件拒绝访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆