上传和读取文件客户端,角度为2

Kat*_*atu 7 javascript client-side typescript angular

我需要用户的日志文件,以便我可以阅读和分析这些文件.例如某些丢弃区域,用户丢弃文件,然后我可以用javascript读取它?

我使用Angular2 rc5.我有node.js运行背面,但我不需要那里的数据.我只在客户端需要它.

是否有可能只使用前端技术读取和解析文件内容,如angular2和javascript?或者我是否必须将文件上传到服务器并在那里进行分析?

Kat*_*atu 17

有可能的!

我最终这样做了.这将读取使用文件对话框选择的所有文件.我不需要将这些发送到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]);
    };
}
Run Code Online (Sandbox Code Playgroud)

这是你如何做到这一点的一个非常基本的例子.