代码之家  ›  专栏  ›  技术社区  ›  Anil

angular7输入类型文件上载不工作

  •  2
  • Anil  · 技术社区  · 6 年前

    现在我工作的角度7输入类型为“文件”不工作。

    安格拉6工作正常。

    角6提交输入文件类型数据

    我会得到这样的字段列表

    enter image description here

    但是角度7只能得到这样的图像路径。

    enter image description here

    只有我更新了角度6到角度7,我会得到这个错误。

    谢谢

    1 回复  |  直到 6 年前
        1
  •  2
  •   rcanpahali    6 年前

    我的Angular7应用程序中有一个上载文档部分,下面是一个工作示例:

    示例.component.html

    <form [formGroup]="form">
    <div class="form-group">
        <label>Select file to upload.</label>
        <input type="file" id="bannedList" (change)="onFileChange($event);" #fileInput>
    </div>
    </form>
    <button type="button" (click)="onSubmit();" [disabled]="!fileInput.value" class="btn btn-success pull-right"><i class="fa fa-save fa-fw"></i> Upload File</button>
    

    示例.component.ts

    import { Component, OnInit, OnDestroy, ElementRef, ViewChild } from '@angular/core';
    import { FormBuilder, FormGroup } from "@angular/forms";
    
    ...
    
    export class exampleUploadComponent implements OnInit, OnDestroy {
      public form: FormGroup;
      @ViewChild('fileInput', { read: ElementRef }) private fileInput: ElementRef;
    
    ...
    
      createForm() {
        this.form = this.fb.group({
          bannedList: null
        });
      }
    
      onFileChange(event) {
        this.uploadStatus = 0;
        if (event.target.files.length > 0) {
          let file = event.target.files[0];
          this.form.get('bannedList').setValue(file);
        }
      }
    
      private prepareSave(): any {
        let input = new FormData();
        input.append('bannedChequeCustomersFile', this.form.get('bannedList').value);
        return input;
      }
    
      onSubmit() {
        const formModel = this.prepareSave();
        this.uploadChequeSubscription = this.chequeOperationService.uploadBannedChequeList(formModel).subscribe(
          (res) => {
            console.log("res handler:", res);
          },
          (err) => {
            console.log("err handler:", err);
          }
        );
      }
    

    推荐文章