HTML文件输入框不会触发fileSelect事件,并警告在onchange事件 - angularJS [英] HTML File input box doesn't fire fileSelect event and caveat in onchange event - angularJS

查看:1254
本文介绍了HTML文件输入框不会触发fileSelect事件,并警告在onchange事件 - angularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的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

这篇关于HTML文件输入框不会触发fileSelect事件,并警告在onchange事件 - angularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆