HTML文件输入框不会触发fileSelect事件,并警告在onchange事件 - angularJS事件、输入框、文件、HTML

2023-09-13 04:19:15 作者:见或不见〃伤都在。

我使用的HTML文件类型控制来选择和上传文件(使用AngularJS)。我的加价是如下:

I'm using HTML File type control to select and upload a file (using AngularJS). My mark-up is as below:

<input id="uploadFile" accept=".csv" type="file" ng-file-select="model.onFileSelect($files)" onchange="angular.element(this).scope().fileNameChanged(this)">

所以,当我选择任何文件fil​​eNameChanged事件被触发,而不是onFileSelect。这工作得很好至今。但是,如果我选择相同的文件上传,将fileNameChanged事件不解雇(因为我上传同一文件),但我想允许用户一次又一次上传同一文件。该onFileSelect事件从来没有发射。

So when I select any file the fileNameChanged event is fired and not onFileSelect. This works fine so far. However if I select the same file to upload, the fileNameChanged event is not fired (because I'm uploading the same file) but I want to allow user to upload the same file again and again. The onFileSelect event is never fired.

解决这个问题的任何方式?

Any way to solve this issue?

推荐答案

终于得到它的工作,在HTML我已经添加的onclick事件JS这样的:

Finally got it to work, in the HTML I've added onclick JS event like this:

<input id="uploadFile" onclick="myFunction();" accept=".csv" type="file" ng-file-select="model.onFileSelect($files)" onchange="angular.element(this).scope().fileNameChanged(this)">

function myFunction() {
  //setting value of null of 0th index coz in my case i allow user to upload only single file.
  //in case of multiple files, you gotta set value to null of the entire array of file list
  //returned by the control
  $('#uploadfile')[0].value = null;
  return true;
}

通过这个,控制将不缓存文件名,并在选择同一个文件onChange事件将被解雇下一次。

With this, the control won't cache the file name and next time when same file is selected the onChange event would get fired.

让我知道,如果你仍然面临的任何问题。

Let me know if you still face any issue.

THX,萨加尔

Thx, Sagar

 
精彩推荐
图片推荐