[英]HTML File input box doesn't fire fileSelect event and caveat in onchange event - angularJS
我正在使用HTML文件類型控件來選擇和上傳文件(使用AngularJS)。 我的標記如下:
<input id="uploadFile" accept=".csv" type="file" ng-file-select="model.onFileSelect($files)" onchange="angular.element(this).scope().fileNameChanged(this)">
因此,當我選擇任何文件時,都會觸發fileNameChanged事件,而不是onFileSelect事件。 到目前為止,這個工作正常。 但是,如果我選擇要上載相同的文件,則不會觸發fileNameChanged事件(因為我正在上載相同的文件),但是我希望允許用戶一次又一次地上載相同的文件。 永遠不會觸發onFileSelect事件。
有什么辦法解決這個問題?
終於成功了,在HTML中,我添加了onclick JS事件,如下所示:
<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事件。
讓我知道您是否仍然遇到任何問題。
Thx,薩加爾
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.