Javascript 使用 AJAX 读取本地文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6923707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:47:05  来源:igfitidea点击:

Using AJAX to read local files

javascriptajax

提问by abhinav singh

I'm a novice to AJAX and just want to confirm: if I have all my code in a folder on my desktop and I am using AJAX to output file content in a div in HTML, is it possible to access local files through AJAX or file should have to be on server?

我是 AJAX 的新手,只想确认:如果我将所有代码都放在桌面上的一个文件夹中,并且我使用 AJAX 在 HTML 的 div 中输出文件内容,是否可以通过 AJAX 或文件必须在服务器上?

I am just testing AJAX functionality for the first time and i am facing problem as its showing error "Access denied " in .js file

我只是第一次测试 AJAX 功能,我遇到了问题,因为它在 .js 文件中显示错误“拒绝访问”

回答by djna

For security reasons JavaScript's access to the file system on the client is restricted - consider whether you would want (somebody else's) JavaScript to read your sensitive documents.

出于安全原因,JavaScript 对客户端文件系统的访问受到限制 - 考虑您是否希望(其他人的)JavaScript 读取您的敏感文档。

Even when experimenting it's best to work with a realistic topology, serve things from the server that would be served from there in the real system.

即使在试验时,最好使用现实的拓扑结构,从真实系统中的服务器提供服务。

It's really easy to set up a web server such as Apache to point to your development directory, so the "server" is just your desktop in disguise. Hence the edit/test cycle is really quick.

设置 Apache 等 Web 服务器指向您的开发目录非常容易,因此“服务器”只是您伪装的桌面。因此,编辑/测试周期非常快。

回答by jAndy

File Access is prohibited from the start, in any browser javascript implementation. Someone can disable that "security feature" in his browser manually. For instance, for Google Chrome you have to startup the executable with --disabled-web-securityas commandline argument. Firefox can disabled that within it's about:config.

在任何浏览器 javascript 实现中,文件访问从一开始就被禁止。有人可以手动禁用浏览器中的“安全功能”。例如,对于 Google Chrome,您必须使用--disabled-web-securityas 命令行参数启动可执行文件。Firefox 可以在它的about:config.

Anyway, you totally cannot rely on that of course if you're writting code for the public. But there is light at the end of the tunnel. The "new" Javascript File APIis already available in Chrome, other vendors will follow soon I guess/hope. That API "officially" allows your script to read files on the local machine.

无论如何,如果您正在为公众编写代码,您当然完全不能依赖它。但隧道尽头有光。“新”JavascriptFile API已经在 Chrome 中可用,我猜/希望其他供应商很快就会跟进。该 API “正式”允许您的脚本读取本地机器上的文件。

回答by user2269911

If you just want it for testing you can try disabling web security on chrome and then it should work.

如果你只是想用它进行测试,你可以尝试在 chrome 上禁用网络安全,然后它应该可以工作。

回答by Pranay Rana

Javascript is work on client side but have limited access so it not able to access local files form the client machine.

Javascript 可在客户端运行,但访问权限有限,因此无法从客户端计算机访问本地文件。

So you require to palce you content on server than you can use ajax and get the data in you div to display the client.

因此,您需要在服务器上放置内容,而不是使用 ajax 并获取 div 中的数据以显示客户端。

回答by Kishore K

I hope its possible to access a file locally using Ajax, i tried it with mozilla firefox and worked well. I'd created 2 text files and paced in the same folder. Here is the code. Sorry if there is any mistake.

我希望可以使用 Ajax 在本地访问文件,我使用 mozilla firefox 进行了尝试并且运行良好。我创建了 2 个文本文件并在同一个文件夹中进行了调整。这是代码。如有错误,请见谅。

function getXmlHttpRequestObject() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest(); //Not IE
    } 
    else if(window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP"); //IE
    } 
    else {
        alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
    }
}           
var receiveReq = getXmlHttpRequestObject();     
function sayHello(fname) {
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
        receiveReq.open("GET", fname, true);
        receiveReq.onreadystatechange = handleSayHello; 
        receiveReq.send(null);
    }           
}
function handleSayHello() {
    if (receiveReq.readyState == 4) {
        document.getElementById('span_result').innerHTML = receiveReq.responseText;
    }
}

Here is the html code
<select name="files" onchange="sayHello(this.value)">
<option value="">Select a file</option>
<option value="file.txt">file.txt</option>
<option value="file2.txt">file2.txt</option>
<option value="ajax.html">Ajax.html</option>
</select><br>
<p>Contents of the file will be displayed below</p>
<div id="span_result"></div>