1

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/

1
  • ` $scope.clearFile = function () { $scope.selectedFileName = null; }` this is working i tried on your jsfiddle Commented Sep 28, 2018 at 13:13

1 Answer 1

1

Try this:

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

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Well this works to the extent the it clears out the UI, but the file is actually there in the file upload. See my updated fiddle: jsfiddle.net/rx6zpqe9 If on upload button click I can still see the file exists.
You are right, I think you should initialise $sope.uploadedFile too $sope.uploadedFile = [];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.