简体   繁体   English

读取本地文本文件js

[英]Read local text files js

So, with my chrome extension I need to read a text file.因此,使用我的 chrome 扩展程序,我需要读取一个文本文件。 I'm trying to use this to just output the text file to the console我正在尝试将此文本文件仅用于 output 到控制台

$.get('text.txt', function(data){
    console.log(data);
});

But in console it outputs the data of the webpage, not my text file at all.但在控制台中它输出网页的数据,而不是我的文本文件。

EDIT- Now I understant how jQuery.get works.编辑-现在我了解 jQuery.get 是如何工作的。 I have my local text file and jQuery.get is meant for server files.我有我的本地文本文件,jQuery.get 用于服务器文件。 I need a way to read local text files.我需要一种读取本地文本文件的方法。 Any help here?这里有什么帮助吗?

You have to specify the dataType (that you're expecting back from the server) as text :您必须将dataType (您期望从服务器返回)指定为text

$.get('text.txt', function(data){
  console.log(data);
}, 'text');

SOLUTION:解决方案:

It will not load files from your local filesystem this way它不会以这种方式从本地文件系统加载文件

You need to put it on a web server and load it from there.您需要将其放在 Web 服务器上并从那里加载。 On the same site as you have the JavaScript loaded from.在与您加载 JavaScript 的站点相同的站点上。

Creating the webserver on localhost should work in Chrome, As it doesn't support reading files from the local filesystem by default.localhost上创建网络服务器应该可以在 Chrome 中工作,因为它默认不支持从本地文件系统读取文件。

Additionally, you can start chrome using option --allow-file-access-from-files, which would allow access to local files.此外,您可以使用选项 --allow-file-access-from-files 启动 chrome,这将允许访问本地文件。

You can read local files like so:您可以像这样读取本地文件:

 function readTextFile(file) { const fReader = new FileReader(); fReader.readAsText(input.files[0]); fReader.onloadend = function(event) { document.getElementById('contents').innerHTML = event.target.result; } } const input = document.getElementById('myfile'); input.addEventListener('change', function() { readTextFile(input.files[0]) });
 <input type="file" id="myfile" name="myfile"> <div id="contents"></div>

Documentation for the FileReader. FileReader 的文档。

You can use FileReader to read files, in this exeple I use a csv file, but you can use txt to.你可以使用FileReader来读取文件,在这个exeple中我使用了一个csv的文件,但是你可以使用txt来读取。

In my case, I use a input with type 'file' and with a onChange I load the file.就我而言,我使用类型为“文件”的输入,并使用 onChange 加载文件。 By other part, I have a ref for indicate the source.另一方面,我有一个 ref 用于指示来源。

<input type="file" class="custom-file-input" ref="doc_categories" onChange="read_file">

In the method 'read_file', I get the first file and I check that exist by his name.在“read_file”方法中,我得到了第一个文件,并通过他的名字检查是否存在。

this.file = this.$refs.doc_categories.files[0];   

if(this.file.name != undefined){
     const reader = new FileReader();
     if (this.file.name.includes(".csv")) {
        reader.onload = (res) => {
            this.content = res.target.result;
        };
        reader.onerror = (err) => console.log(err);
        reader.readAsText(this.file);
     } else {
       this.content = "check the console for file output";
       reader.onload = (res) => {
         console.log(res.target.result);
       };
       reader.onerror = (err) => console.log(err);
       reader.readAsText(this.file);
     }
}else{
  $.notify('Error to read the file.');
}

This code comes from vue code and I tried to migrate to js vanilla.此代码来自 vue 代码,我尝试迁移到 js vanilla。 I hope it helped you.希望对您有所帮助。

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

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