Angular js Input type file - clear previously selected file

32,927

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);
};
Share:
32,927
KeenUser
Author by

KeenUser

Updated on July 19, 2022

Comments

  • KeenUser
    KeenUser almost 2 years

    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>
    
  • Ch Faizan Mustansar
    Ch Faizan Mustansar over 8 years
    Yes, indeed this is an easiest way.... But it is not the right way. I have gone through the trouble of rewriting the whole code once again in the form of directives and all, thats why I cannot recommend this approach.
  • Eddy
    Eddy over 7 years
    This complained that it cannot use jqLite for selectors. But this worked fine: var myEl = angular.element( document.querySelector( '#some-id' ) );
  • Mawg says reinstate Monica
    Mawg says reinstate Monica over 7 years
    If you want a separate Clear or Cancel button, add an id= to the element. E.g. id="fileSelector_1" and then angular.element(document.querySelector('#fileSelector_1')).v‌​al(null);` This works for me. I use it to clear on both the Save and Cancel buttons,