简体   繁体   English

如何在 Angular 8 的 FileReader 中从 2 个不同的 HTML 输入中获取 2 个文件?

[英]How to get 2 files from 2 different HTML inputs in FileReader in Angular 8?

In my current project 2 JSON files have to be put together.在我当前的项目中,必须将 2 个 JSON 文件放在一起。 In order to be able to use all files, they can be integrated via 2 HTML inputs.为了能够使用所有文件,它们可以通过 2 个 HTML 输入进行集成。 I used the JS FileReader to edit the two files.我使用 JS FileReader 来编辑这两个文件。

Is there a way to make both files available at the same time?有没有办法让两个文件同时可用?

HTML: HTML:

<input (change)="onFileChange($event)" [(ngModel)]="json1" type="file">
<input (change)="onFileChange($event)" [(ngModel)]="json2" type="file">

Component:成分:

public onFileChange(event) {
    var one = this.json1;
    var two = this.json2;
    one = event.target.files;
    two = event.target.files;
    if (one) {
      var i = 0, f; f = one[i]; i++;
      console.log(i);
    } else {
      var i = 0, f2; f2 = two[i]; i++;
      console.log(i);
    }
    var fileReader = new FileReader();
    fileReader.onload = function(fileLoadedEvent) {
        var showJSON = fileLoadedEvent.target.result;
    };
    fileReader.readAsText(f, "UTF-8");
    console.log(f);
  };

My current idea is to give each file a number which you can then work with, only so far this has not worked.我目前的想法是给每个文件一个编号,然后您可以使用它,但到目前为止这还没有奏效。

Add multiple attribute to input element为输入元素添加multiple属性

<input type="file" multiple (change)="onFileChange(myInput)" #myInput>

and access using并使用访问

onFileChange(myInput) {
  console.log(myInput.files) //You will get array of files here loop through each 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM