如何使用Angularjs在ie浏览器中上传文件后清除文件名

How to clear the file name after file upload in Internet Explorer using Angularjs

本文关键字:文件 清除 文件名 Angularjs 何使用 ie 浏览器      更新时间:2023-09-26
<input type="file" ng-model="myobj.file" ng-file-select="onFileSelect_filter($files)" id="fileButton" >
<button type="button" ng-click="start_Filter();" >Upload</button>

和我的onFileSelect_filter()函数是这样的

$scope.onFileSelect_filter = function($files){
   ....
};

至少在Internet Explorer中单击Upload按钮后,我想清除文件名。

此输入框不在表单内提前感谢!!

不幸的是,在这种情况下,将绑定的$scope变量等同于空字符串或null将不起作用。

尽管你可以通过下面提到的方法之一来实现它。

方法1:

您必须在单击时(在您的情况下是在上传时)替换DOM上的html,并将其恢复到上传前的状态。就像. .

 $scope.start_Filter = function(){
   // This is in the upload function post all your logic
   $("#fileButton").replaceWith("<input type='file' ng-model='myobj.file' ng-file-select='onFileSelect_filter($files)' id='fileButton' >").html();
};

注意:replaceWith函数

中的所有HTML属性值必须使用单引号而不是双引号方法2:

或者,您可以使用JQuery轻松地清除文件名。就像. .

$scope.start_Filter = function(){
   // This is in the upload function post all your logic
   $("#fileButton").val('');
};

其中一个应该可以。

请尝试一下,如果有什么问题请告诉我。

最简单的方法是:angular.element("input[type='file']").val(null);

在您的控制器中,在文件上传完成后更改myobj.file value.

$scope.myobj.file = ''

尝试清除$作用域。myobj var:

$scope.onFileSelect_filter = function($files){
    $scope.myobj.file = undefined;
};