【问题标题】:Is it possible to upload a JSON file though file upload and read the contents in Angular 6 without talking to an API?是否可以通过文件上传上传 JSON 文件并在 Angular 6 中读取内容而不与 API 对话?
【发布时间】:2018-12-07 09:25:21
【问题描述】:

将下载的 JSON 文件作为结构。希望能够通过 Angular 中的文件上传(使用 Angular 6)上传它,然后直接在 Angular 中读取文件的内容,而不是将其上传到 API。

搜索类似的接缝以带回如何在 Angular 中读取服务器上的本地文件,而不是通过文件上传。

【问题讨论】:

标签: javascript angular typescript angular6


【解决方案1】:

您可以尝试以下方法:

//In your Template:
<input type="file" name="files" (change)="uploadFile($event)" />

//In your component:
uploadFile(event) {
  if (event.target.files.length !== 1) {
    console.error('No file selected');
  } else {
    const reader = new FileReader();
    reader.onloadend = (e) => {
      // handle data processing
      console.log(reader.result.toString());
    };
    reader.readAsText(event.target.files[0]);
  }
}

我创建了这个demo。看看是否是您需要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2018-09-21
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多