Search code examples
angularjsng-file-upload

AngularJS ng-upload reset file selection


I am using a simple file upload as below:

<button type="file" ngf-select ng-model="fileData"
        ng-change="fileChanged(fileData)" name="file"  required >
    Select File
</button>

And I have another button which when clicked I want to clear out the file that was selected.

<button type="button" class="btn btn-primary" ng-click="clearFile()">
    Clear
</button>

I have the Controller code for button click as:

$scope.fileChanged = function(fileData) {
  if (fileData != undefined) {
    $scope.selectedFileName = fileData.name;
   }
}  

$scope.clearFile = function () {
        //None of these works
        //angular.element("input[type='file']").val(null);
       // $scope.fileData = [];
}

I have tried couple of options as I searched through previous posts but none of it works. What am I missing here.

Here is my jsfiddle: http://jsfiddle.net/abco2Lp0/


Solution

  • Try this:

    $scope.clearFile = function () {
       $scope.fileData = [];
       $scope.selectedFileName = null;
       $scope.uploadedFile = [];
    }
    

    Hope this helps.