简体   繁体   English

使用本地计算机上的js / html文件读取本地文本文件

[英]Read local text file using js/html file on on local machine

I've built a simple html page with javascript in a separate file, called on a button press. 我用一个单独的文件构建了一个简单的html页面,其中包含javascript,按下按钮即可调用。

I've opened the html file in chrome, and the path resembles: file:///home/tom/projects/index.html 我已经在chrome中打开了html文件,其路径类似于:file:///home/tom/projects/index.html

The javascript needs to read a JSON file (file:///home/tom/projects/mydata.json) which is in the same directory, using a hardcoded path. javascript需要使用硬编码路径读取同一目录中的JSON文件(file:///home/tom/projects/mydata.json)。

I'm really struggling to do this. 我真的很努力做到这一点。 As I understand, this is because I'm using client side js (meaning I can't use the fs library for example), which is limiting my options. 据我了解,这是因为我正在使用客户端js(例如,我不能使用fs库),这限制了我的选择。

According to the question here , I can't load the file if I use the URL in the format: file:///home/to.... as it gives me the error: 根据这里的问题,如果我使用以下格式的URL,则无法加载文件: file:///home/to....因为它给了我错误:

Cross origin requests are only supported for protocol schemes: HTTP, data, chrome, chrome-extension, https. 跨源请求仅支持以下协议方案:HTTP,数据,chrome,chrome扩展名,https。

If I start an HTTP-server, as the answer suggests, I can use server-side modules, but if possible I would like to avoid this. 如果启动HTTP服务器(如答案所示),则可以使用服务器端模块,但如果可能的话,我希望避免这种情况。

I've noticed many answers that suggest using a dialog box like this : 我注意到很多答案使用像一个对话框,提示

var selectedFile = document.getElementById('input').files[0];
function readFile (file_path) {       
    var reader = new FileReader();
    reader.readAsText(file_path);
    console.log(reader.substring(0, 100));
 };

but I can't make this work with a path in the form: file:///home/tom/projects/mydata.json 但是我无法使用以下形式的路径来工作: file:///home/tom/projects/mydata.json

Is there a way to load a .json file from a file:///home/to.... format URL using client-side javascript, with a hardcoded path (ie not asking the user to select the file from a selection box)? 有没有一种方法可以使用客户端JavaScript从file:///home/to....格式的URL加载.json文件,并使用硬编码路径(即不要求用户从选择框中选择文件) )?

This is a deliberate security restriction, to stop a user from being given, and then opening, a HTML page which then tries to read their disk. 这是蓄意的安全限制,以阻止授予用户然后打开一个HTML页,然后尝试读取其磁盘。

Run your page in a webserver (as that question suggested) then you can either load the JSON from a URL (eg something like http://localhost/projects/mydata.json ) using JavaScript, or use a server-side language to fetch it and display it inside the rendered HTML. 在网络服务器上运行页面(如该问题所建议),然后您可以使用JavaScript从URL(例如, http://localhost/projects/mydata.json )加载JSON,或使用服务器端语言来获取并将其显示在呈现的HTML中。 Either way will work, the first way is probably simpler and closest to what you've got now. 无论哪种方法都可以,第一种方法可能更简单并且最接近您现在所拥有的。

It's always far better to serve HTML pages from a HTTP server, the way it's intended to be. 从HTTP服务器为HTML页面提供服务始终要好得多。

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

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