javascript 如果文件名相同,则更改事件功能不适用于输入类型文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46970952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:05:00  来源:igfitidea点击:

Change event function not working on input type file if file name is same

javascripthtmlangular

提问by user1881845

This is my html code to upload excel file

这是我上传excel文件的html代码

 <span class="control-fileupload" >
      <label for="file1" class="text-left">{{filePlaceHolder}}</label>
      <input type="file" id="file1" value="" (change)="openFile($event)" >
 </span>

But the problem is if i'm uploading same file twice the change function is not executing again because there is no change in input field.

但问题是,如果我上传相同的文件两次,更改功能不会再次执行,因为输入字段没有更改。

Suppose i have uploaded abc.xls file once and there are some validation on this file and if i change the content of abc.xls and re upload it then change function is not re validating it again.

假设我已经上传了一次 abc.xls 文件并且对该文件进行了一些验证,如果我更改了 abc.xls 的内容并重新上传它,那么更改功能不会再次重新验证它。

What changes i should make to work change function every time i upload a file whether file name is same or not.

每次上传文件时,无论文件名是否相同,我应该对工作更改功能进行哪些更改。

I want to know how to write this click function in type script as i'm new to this.

我想知道如何在类型脚本中编写这个点击函数,因为我是新手。

回答by guramidev

In angular 2 you can do it like this:

在 angular 2 中,您可以这样做:

<span class="control-fileupload" >
      <label for="file1" class="text-left">{{filePlaceHolder}}</label>
      <input #fileInput type="file" id="file1" (click)="fileInput.value = null" value="" (change)="openFile($event)" >
 </span>

This way every time you click on file input it will clear it's value so even if you select the same file change will fire.

这样每次单击文件输入时,它都会清除它的值,因此即使您选择相同的文件更改也会触发。