最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to upload a JSON file though file upload and read the contents in Angular 6 without talking to an AP

programmeradmin0浏览0评论

Have a downloaded JSON file as a structure. Want to be able to upload it though file upload in Angular (Using Angular 6) and then read the contents of the file directly in Angular, rather than uploading it to an API.

Searching for similar seams to bring back how to read a local file on a server in Angular, rather than through file upload.

Have a downloaded JSON file as a structure. Want to be able to upload it though file upload in Angular (Using Angular 6) and then read the contents of the file directly in Angular, rather than uploading it to an API.

Searching for similar seams to bring back how to read a local file on a server in Angular, rather than through file upload.

Share Improve this question edited Dec 7, 2018 at 9:40 Tushar Walzade 3,8194 gold badges37 silver badges58 bronze badges asked Dec 7, 2018 at 9:25 Dan ReilDan Reil 3871 gold badge3 silver badges17 bronze badges 1
  • Are you looking for the File API? I highly remend this guide: html5rocks./en/tutorials/file/dndfiles – Matthew Herbst Commented Dec 7, 2018 at 9:30
Add a ment  | 

1 Answer 1

Reset to default 9

You can try the following:

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

//In your ponent:
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]);
  }
}

I created this demo. Have a look and check if that is what you need.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论