如何在Angular 2中使用输入标记文件类型重置所选文件?

时间:2016-10-20 22:04:26

标签: angular

这是我的输入标签的样子:

<input type="file" placeholder="File Name" name="filename" (change)="onChange($event)">
<button>Reset</button>

我想在Angular 2中重置所选文件。非常感谢帮助。如果您需要更多详细信息,请与我们联系。

P.S。

我可以从$event参数中获取文件详细信息并将其保存在typescript变量中,但此变量未绑定到输入标记。

13 个答案:

答案 0 :(得分:137)

您可以使用#someValue访问组件中的输入。首先,您需要将<input #myInput type="file" placeholder="File Name" name="filename" (change)="onChange($event)"> 添加到输入中,以便在组件中读取它:

ViewChild

然后在您的组件中,您需要从@angular/core导入import { ViewChild } from '@angular/core';

ViewChild

然后使用@ViewChild('myInput') myInputVariable: ElementRef; 访问模板中的输入:

myInputVariable

现在您可以使用#myInput重置所选文件,因为它是对reset()输入的引用,例如将在click事件上调用的创建方法reset() { console.log(this.myInputVariable.nativeElement.files); this.myInputVariable.nativeElement.value = ""; console.log(this.myInputVariable.nativeElement.files); } 按钮:

console.log

首先console.log将打印您选择的文件,第二个this.myInputVariable.nativeElement.value = "";将打印一个空数组,因为this.myInputVariable.nativeElement.value = "";会从输入中删除所选文件。我们必须使用FileList来重置输入的值,因为输入的sbt属性只读,因此无法从数组中删除项目。这是有效的Plunker

答案 1 :(得分:11)

实现它的一种方法是将输入包装在<form>标记中并重置它。

我不考虑将thr表格附加到NgFormFormControl

@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

答案 2 :(得分:8)

Angular 5

<强> HTML

<input type="file" #inputFile>

<button (click)="reset()">Reset</button>

<强>模板

@ViewChild('inputFile') myInputVariable: ElementRef;

reset() {
    this.myInputVariable.nativeElement.value = '';
}

不需要按钮。您可以在更改事件后重置它,它仅用于演示

答案 3 :(得分:2)

简短版本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 {


}

我认为更常见的情况是不使用按钮但会自动重置。 Angular Template statements支持链接表达式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 );
  }

}

interesting link关于为什么没有值变化的递归。

答案 4 :(得分:2)

我认为很简单,使用#variable

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

“variable.value = null”选项也可用

答案 5 :(得分:1)

我通常在捕获所选文件后重置我的文件输入。无需按下按钮,即可将$event对象中的所有内容传递给onChange

onChange(event) {
  // Get your files
  const files: FileList = event.target.files;

  // Clear the input
  event.srcElement.value = null;
}

不同的工作流程,但OP没有提到按下按钮是一个要求......

答案 6 :(得分:1)

我的情况如下:

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

我正在HTML中使用#fileInput而不是在component.ts中创建ViewChild来重置它。 每当单击“上传文件”按钮时,它都会首先重置#fileInput,然后触发#fileInput.click()函数。 如果需要重置任何单独的按钮,则可以单击#fileInput.value=''

答案 7 :(得分:0)

如果您遇到ng2-file-upload问题,

HTML:

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

组件

import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';

@ViewChild('`**activeFrameinputFile**`') `**InputFrameVariable**`: ElementRef;

this.frameUploader.onSuccessItem = (item, response, status, headers) => {
    this.`**InputFrameVariable**`.nativeElement.value = '';
   };

答案 8 :(得分:0)

模板:

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

组件:

fileChange(event) {
        alert(this.torrentFileValue);
        this.torrentFileValue = '';
    }
}

答案 9 :(得分:0)

N
<input type="file" id="image_control" (change)="validateFile($event)" accept="image/gif, image/jpeg, image/png" />

答案 10 :(得分:0)

我已将此输入标签添加到表单标签中。

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

我是角型打字稿,我在以下几行中添加了代码,并在文档表单中获取了表单ID,并将该值设置为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'] = "";
              }
      }
    }

在控制台中打印document.forms,您可以了解想法。

答案 11 :(得分:0)

您可以使用模板引用变量并发送给方法

html

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

组件

onChange(event: any, element): void {
    // codes
    element.value = '';
  }

答案 12 :(得分:0)

我正在使用一个非常简单的方法。上传文件后,我不久将使用* ngIf删除输入控件。这将导致从dom中删除输入字段并对其进行重新添加,因此这是一个新控件,因此它是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">