使用angular 2在客户端上传和读取文件

Upload and read file client side, with angular 2

本文关键字:读取 文件 客户端 angular 使用      更新时间:2023-09-26

我需要来自用户的日志文件,以便我可以读取和分析这些文件。例如,某种类型的拖放区域,在用户拖放文件,然后我可以读取它与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]);
    };
}

这是你如何做的一个非常基本的例子。