How to reset selected file with input tag file type in Angular 2?

前端 未结 13 1882
北恋
北恋 2020-12-12 14:56

This is how my input tag looks like:


相关标签:
13条回答
  • 2020-12-12 15:23

    One way to achieve it is to wrap your input in <form> tag and reset it.

    I'm not considering attaching thr form to NgForm or FormControl either.

    @Component({
      selector: 'form-component',
      template: `
        <form #form>
          <input type="file" placeholder="File Name" name="filename">
        </form>
        <button (click)="reset()">Reset</button>
    `
    })
    class FormComponent {
    
    
    
      @ViewChild('form') form;
    
    
      reset() {
        this.form.nativeElement.reset()
      }
    }
    

    https://plnkr.co/edit/Ulqh2l093LV6GlQWKkUA?p=preview

    0 讨论(0)
  • 2020-12-12 15:24

    I am using a a very simple aproach. After a file has been uploaded, i shortly remove the input control, using *ngIf. That will cause the input field being removed from the dom and re-added, consequencely it is a new control, and therefore it is emply:

    showUploader: boolean = true;
    
    async upload($event) {
      await dosomethingwiththefile();
      this.showUploader = false;
      setTimeout(() =>{ this.showUploader = true }, 100);
    }
    <input type="file" (change)="upload($event)" *ngIf="showUploader">

    0 讨论(0)
  • 2020-12-12 15:25

    If you are facing issue with ng2-file-upload,

    HTML:

    <input type="file" name="myfile" ` **#activeFrameinputFile** `ng2FileSelect [uploader]="frameUploader" (change)="frameUploader.uploadAll()" />
    

    component

    import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
    
    @ViewChild('`**activeFrameinputFile**`') `**InputFrameVariable**`: ElementRef;
    
    this.frameUploader.onSuccessItem = (item, response, status, headers) => {
        this.`**InputFrameVariable**`.nativeElement.value = '';
       };
    
    0 讨论(0)
  • 2020-12-12 15:29

    You can use ViewChild to access the input in your component. First, you need to add #someValue to your input so you can read it in the component:

    <input #myInput type="file" placeholder="File Name" name="filename" (change)="onChange($event)">
    

    Then in your component you need to import ViewChild from @angular/core:

    import { ViewChild } from '@angular/core';
    

    Then you use ViewChild to access the input from template:

    @ViewChild('myInput')
    myInputVariable: ElementRef;
    

    Now you can use myInputVariable to reset the selected file because it's a reference to input with #myInput, for example create method reset() that will be called on click event of your button:

    reset() {
        console.log(this.myInputVariable.nativeElement.files);
        this.myInputVariable.nativeElement.value = "";
        console.log(this.myInputVariable.nativeElement.files);
    }
    

    First console.log will print the file you selected, second console.log will print an empty array because this.myInputVariable.nativeElement.value = ""; deletes selected file(s) from the input. We have to use this.myInputVariable.nativeElement.value = ""; to reset the value of the input because input's FileList attribute is readonly, so it is impossible to just remove item from array. Here's working Plunker.

    0 讨论(0)
  • 2020-12-12 15:31

    I think its simple, use #variable

    <input #variable type="file" placeholder="File Name" name="filename" (change)="onChange($event); variable.value='' ">
    <button>Reset</button>
    

    "variable.value = null" option also available

    0 讨论(0)
  • 2020-12-12 15:32

    template:

    <input [(ngModel)]="componentField" type="file" (change)="fileChange($event)" placeholder="Upload file">
    

    component:

    fileChange(event) {
            alert(this.torrentFileValue);
            this.torrentFileValue = '';
        }
    }
    
    0 讨论(0)
提交回复
热议问题