简体   繁体   English

使用js从本地存储读取文件

[英]read file from local storage using js

hi i'm using java script to read txt file from my pc. 嗨,我正在使用Java脚本从我的电脑读取txt文件。 i try to do it in 3 ways like that: 我尝试通过3种方式做到这一点:

function readTextFile(file) {
            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(allText);
                    }
                }
            }
            rawFile.send(null);
        }
        console.log("1");
        readTextFile("text.txt"); //GOOD
        console.log("2");
        readTextFile("D:/Documents/Documents/Aptana Studio 3 Workspace/Yonit_web_services/test/text.txt"); //BAD
        console.log("3");
        readTextFile("file:///D:/Documents/Documents/Aptana Studio 3 Workspace/Yonit_web_services/test/text.txt"); //BAD

all of them are pointing at the same file i got an error like that: 他们所有人都指向同一个文件,我得到这样的错误:

XMLHttpRequest cannot load file:///D:/Documents/Documents/Aptana%20Studio%203%20Workspace/Yonit_web_services/test/text.txt. XMLHttpRequest无法加载文件:/// D:/Documents/Documents/Aptana%20Studio%203%20Workspace/Yonit_web_services/test/text.txt。 Cross origin requests are only supported for HTTP. 跨源请求仅支持HTTP。 test.html:21readTextFile test.html:21(anonymous function) test.html:26 test.html:21readTextFile test.html:21(匿名函数)test.html:26

whay is that ? 那是什么? thanks. 谢谢。

I had the same problem a while back. 不久前,我遇到了同样的问题。 I fixed it by opening Chrome through Windows command prompt with this parameter: --disable-web-security 我通过使用以下参数通过Windows命令提示符打开Chrome来修复此问题:-- --disable-web-security

Please note that this has security vulnerabilities. 请注意,这具有安全漏洞。

Browser does not allow to file access through javascript due to security reasons, find the link below. 出于安全原因,浏览器不允许通过javascript文件访问,请找到以下链接。

Security 安全

But in HTML5 its possible via using File Upload/ Download, link File Uplaod/Download 但是在HTML5中,可以通过使用文件上传/下载,链接文件更新/下载来实现

The first method of reading the file: 读取文件的第一种方法:

readTextFile("text.txt"); //GOOD

is working because "text.txt" is a relative path in the same respect that if you had the file system: 之所以起作用,是因为"text.txt"是相对路径(与使用文件系统时相同):

\index.html
\img\img.png

img.png would be accessible to index.html in this respect you might be able to access files and folders in parent and parrent's parent directories by using relative path-ing. 在这方面,index.html可以访问img.png,您可以使用相对路径访问父级和父级的父级目录中的文件和文件夹。

from a windows command prompt you can test these relative paths with dir as follows: 在Windows命令提示符下,您可以使用dir测试这些相对路径,如下所示:

REM Dir will show contents of current directory
Dir
REM Dir .. will show contents of parent directory
Dir ..
REM Dir ../.. will show contents of parent's parent directory
Dir ../..

the same path-ing should work in JavaScript 同样的路径应该在JavaScript中起作用

This being said, the restrictions on d:/ and file://D:/ are most likely for security reason. 话虽如此,出于安全原因,最有可能对d:/file://D:/进行限制。 You might want to check out http://en.wikipedia.org/wiki/Samba_%28software%29 for more details. 您可能想查看http://en.wikipedia.org/wiki/Samba_%28software%29了解更多详细信息。

您总是可以做一件事,并将其放在应用程序从其访问的某个URL上(仅当它不包含任何安全信息时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM