是否有机会检测用户为文件元素类型的HTML输入所做的每个文件选择?

这个问题以前被问过很多次,但是如果用户再次选择相同的文件,通常建议的onchange事件不会触发。


当前回答

如果你使用的是Angular,双向绑定对我来说是有效的。

这是我的HMTL

<input type="file" #upload name="upload" [(ngModel)]="inputValue"(change)='fileEvent($event)'/>

和TS . .

  @ViewChild('upload') uploadBtn: HTMLElement;

  fileEvent(e: any){
    
   //file upload part... 

    this.inputValue = "";
  }

其他回答

在文件成功加载后,可以做任何您想做的事情。完成文件处理后,将文件控制的值设置为空白字符串。因此.change()将始终被调用,即使文件名更改与否。举个例子,你可以做这件事,对我来说很有魅力

   $('#myFile').change(function () {
       LoadFile("myFile");//function to do processing of file.
       $('#myFile').val('');// set the value to empty of myfile control.
    });
<input #myInput type="file" id="imgFile" (click)="$event.target.value=null"
       (change)="detectUploadedImage($event)" accept="image/*" />
handleChange({target}) {
    const files = target.files
    target.value = ''
}

选择的答案(使用状态设置输入值null)给了我一个错误。

我使用空字符串代替

    const [fileValue, setFileValue] = React.useState("")

    <input
        onClick={() => {
           setFileValue("");
        }}
        type="file"
        value={fileValue}
        onChange={handleAddFile}
    />


如果你使用的是Angular,双向绑定对我来说是有效的。

这是我的HMTL

<input type="file" #upload name="upload" [(ngModel)]="inputValue"(change)='fileEvent($event)'/>

和TS . .

  @ViewChild('upload') uploadBtn: HTMLElement;

  fileEvent(e: any){
    
   //file upload part... 

    this.inputValue = "";
  }