简体   繁体   English

读取通过FileSaver.js保存的文件

[英]Read a file saved via FileSaver.js

Good Day, 美好的一天,

I'm still new to programming and I'm using FileSaver.js to save a .txt file. 我仍然是编程的新手,并且正在使用FileSaver.js保存.txt文件。

I managed to make the app I'm building saving the file in my "Downloads" folder, but now I hit a wall when I want to read this file. 我设法使正在构建的应用程序将文件保存在“下载”文件夹中,但是现在当我想读取该文件时遇到了麻烦。

I understand, for security reasons, we cannot access to the whole computer, but it is the case here ? 我了解,出于安全原因,我们无法访问整个计算机,但是这里是这种情况吗?

I'm using the FileReader() method but I'm not able to access the file... I went there https://www.html5rocks.com/en/tutorials/file/filesystem/ and here https://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files ... But unfortunately I don't understand the way to access the file and to read it. 我正在使用FileReader()方法,但无法访问该文件...我去了https://www.html5rocks.com/en/tutorials/file/filesystem/ ,在这里https:// www .html5rocks.com / zh-CN / tutorials / file / dndfiles /#toc-reading-files ...但是很遗憾,我不了解访问文件和读取文件的方式。

I save the file this way (using FileSaver.js): 我以这种方式保存文件(使用FileSaver.js):

var blob = new Blob([data], {type:"text/plain;charset=utf-8"});
saveAs(blob, filename);

When it's time to load the file, I do that: 是时候加载文件了,我这样做:

var fileToLoad = filename;
var fileReader = new FileReader();
fileReader.onload = function(fileLoadEvent) {
   console.log("File loaded properly!");
};
content = fileReader.readAsText(fileToLoad);

I would like to put the content of the file into the variable content . 我想将文件的内容放入变量content

If anybody can help me to clarify this... Thank You in advance! 如果有人可以帮助我澄清这一点...预先谢谢!

content = fileReader.readAsText(fileToLoad); doesn't return the content, it simply triggers the asynchronous parsing of the file as text. 不返回内容,它只是触发文件作为文本的异步解析。 And only later when the filereader is finished reading the content, you can access it in the onload event object like this: 而且只有在文件读取器完成内容读取之后,您才可以在onload事件对象中访问它,如下所示:

var fr = new FileReader();
fr.onload = function(e) {
    content = e.target.result; // here is the loaded content;
};
fr.readAsText(fileToLoad);

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

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