上传和读取文件客户端,角度为2 [英] Upload and read file client side, with angular 2
问题描述
我需要用户的日志文件,以便我可以阅读和分析这些文件.例如某种放置区域,用户放置文件,然后可以使用javascript读取文件?
I need log file(s) from user so I can read and analyze those. For example somekind of drop area, where user drops a file, then I can read it with javascript?
我使用Angular2 rc5.我有node.js在后台运行,但是我不需要那里的数据.我只在客户端需要它.
I use Angular2 rc5. I have node.js running backside, but I don't need the data there. I need it only at client side.
是否可以仅使用前端技术(例如angular2和javascript)读取和解析文件内容?还是我必须将文件上传到服务器并在那里进行分析?
Is it possible to read and parse file content with just frontend tech, like angular2 and javascript? Or do I have to upload the file to server and analyze it there?
推荐答案
有可能!
我最终这样做了.这将读取通过文件对话框选择的所有文件.我不需要将这些发送到node.js.我可以在客户端上操作这些.
I ended up doing it like this. This reads all the files that are selected with file dialog. I don't need to send these to node.js. I can just manipulate these on client.
<input type='file' accept='text/plain' multiple (change)='openFile($event)'>
openFile(event) {
let input = event.target;
for (var index = 0; index < input.files.length; index++) {
let reader = new FileReader();
reader.onload = () => {
// this 'text' is the content of the file
var text = reader.result;
}
reader.readAsText(input.files[index]);
};
}
这是如何执行此操作的非常基本的示例.
It is very basic example of how you can do it.
这篇关于上传和读取文件客户端,角度为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!