[英]Upload and read file client side, with angular 2
我需要用户的日志文件,以便我可以阅读和分析这些文件。 例如某些丢弃区域,用户丢弃文件,然后我可以用javascript读取它?
我使用Angular2 rc5。 我有node.js运行背面,但我不需要那里的数据。 我只在客户端需要它。
是否有可能只使用前端技术读取和解析文件内容,如angular2和javascript? 或者我是否必须将文件上传到服务器并在那里进行分析?
有可能的!
我最终这样做了。 这将读取使用文件对话框选择的所有文件。 我不需要将这些发送到node.js. 我可以在客户端操纵这些。
<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]);
};
}
这是你如何做到这一点的一个非常基本的例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.