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

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

This is how my input tag looks like:


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

    I typically reset my file input after capturing the selected files. No need to push a button, you have everything required in the $event object that you're passing to onChange:

    onChange(event) {
      // Get your files
      const files: FileList = event.target.files;
    
      // Clear the input
      event.srcElement.value = null;
    }
    

    Different workflow, but the OP doesn't mention pushing a button is a requirement...

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

    I have add this input tag into form tag..

     <form id="form_data">
      <input  type="file" id="file_data"  name="browse"
      (change)="handleFileInput($event, dataFile, f)"  />
    </form>
    

    I angular typescript, I have added below lines, get your form id in document forms and make that value as null.

        for(let i=0; i<document.forms.length;i++){
          if(document.forms[i].length > 0){
                 if(document.forms[i][0]['value']){ //document.forms[i][0] = "file_data"
                     document.forms[i][0]['value'] = "";
                  }
          }
        }
    

    Print document.forms in console and you can get idea..

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

    In my case I did it as below:

     <input #fileInput hidden (change)="uploadFile($event.target.files)" type="file" />
     <button mat-button color="warn" (click)="fileInput.value=''; fileInput.click()"> Upload File</button>
    
    

    I am resetting it using #fileInput in HTML rather than creating a ViewChild in component.ts . Whenever the "Upload File" button is being clicked, first it resets the #fileInput and then triggers #fileInput.click() function. If any separate button needed to reset then on click #fileInput.value='' can be executed.

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

    Angular 5

    html

    <input type="file" #inputFile>
    
    <button (click)="reset()">Reset</button>
    

    template

    @ViewChild('inputFile') myInputVariable: ElementRef;
    
    reset() {
        this.myInputVariable.nativeElement.value = '';
    }
    

    Button is not required. You can reset it after change event, it is just for demonstration

    0 讨论(0)
  • 2020-12-12 15:18
    <input type="file" id="image_control" (change)="validateFile($event)" accept="image/gif, image/jpeg, image/png" />
    
    validateFile(event: any): void {
        const self = this;
        if (event.target.files.length === 1) {
          event.srcElement.value = null;
        }
      }
    
    0 讨论(0)
  • 2020-12-12 15:21

    Short version Plunker:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      template: `
          <input #myInput type="file" placeholder="File Name" name="filename">
          <button (click)="myInput.value = ''">Reset</button>
      `
    })
    export class AppComponent {
    
    
    }
    

    And i think more common case is to not using button but do reset automatically. Angular Template statements support chaining expressions so Plunker:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      template: `
          <input #myInput type="file" (change)="onChange(myInput.value, $event); myInput.value = ''" placeholder="File Name" name="filename">
      `
    })
    export class AppComponent {
    
      onChange(files, event) {
        alert( files );
        alert( event.target.files[0].name );
      }
    
    }
    

    And interesting link about why there is no recursion on value change.

    0 讨论(0)
提交回复
热议问题