Angular js Input type file - clear previously selected file

KeenUser picture KeenUser · Jul 27, 2015 · Viewed 31.9k times · Source

I am using input type="file" for my upload control using angular js. Everytime I click on browse, I do not want to see the previously selected file. By default,this seems to be retained. Can it be achieved by writing a directive? Can it be triggered everytime I click on browse?

I am using a bootstrap implementation to replace default browse button with some better.

 <span class="useBootstrap btn btn-default btn-file">
                Browse <input type="file"  />
            </span>

Answer

KeenUser picture KeenUser · Jul 30, 2015

This was the easiest way, without using any directive or complex code.

Browse <input type="file" ng-click="clear()" 

And in your controller

 $scope.clear = function () {
    angular.element("input[type='file']").val(null);
};