如何获得多个输入文件中的对象数组功能?

时间:2018-12-14 07:57:40

标签: javascript html typescript angular6

我的项目使用Angular6创建一个多上传图片文件,并且 我想创建一个数组对象,像这样发送到数据库

  public foodmenu = [
  { "name": "hamburger", "img": "base64"},
  { "name": "pizza", "img": "base64" },
  ];

food-manage.component.html

<div class="form-group">
  <label class="control-label">upload : </label>
  <input class="ml-3" input type="file" id="files" multiple 
  (change)="getFileDetails($event)">
</div>
<div *ngFor="let item of myFilesName">nameMenu : {{item}}</div>
<output id="list"></output>

food-manage.component.ts

  getFileDetails(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {
      var menuDetail = {};
      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();



      // Closure to capture the file information.
      reader.onload = (function (theFile) {
        return function (e) {
          menuDetail['img'] = e.target.result;
          menuDetail['name'] = theFile.name.replace(/\.[^/.]+$/, "");
          console.log(menuDetail);
          var detail = document.createElement('container');
          detail.innerHTML = ['<div class="row"><div class="col-md-6 col-xs-12"><div class="card" style="width: 18rem;"><img class="card-img-top" src="', e.target.result,
            '" title="', escape(theFile.name), '"/><div class="card-body"> <h5 class="card-title">', theFile.name.replace(/\.[^/.]+$/, ""), '</h5></div></div></div></div>'].join('');
          document.getElementById('list').insertBefore(detail, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);

    }
  }
  

结果

enter image description here

0 个答案:

没有答案