【问题标题】:Upload and read file client side, with angular 2上传和读取文件客户端,角度为 2
【发布时间】:2017-01-10 21:13:10
【问题描述】:

我需要用户的日志文件,以便我可以阅读和分析这些文件。例如某种放置区域,用户放置一个文件,然后我可以用 javascript 读取它?

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

是否可以仅使用 angular2 和 javascript 等前端技术来读取和解析文件内容?还是我必须将文件上传到服务器并在那里进行分析?

【问题讨论】:

标签: javascript angular typescript client-side


【解决方案1】:

有可能!

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

这是您如何做到这一点的非常基本的示例。

【讨论】:

  • 这种方法在打字稿中不起作用,因为文件阅读器只读取一个文件而不是多个文件。
  • 终于,第一次尝试就奏效了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 2011-03-15
  • 2012-02-03
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多