1

I am new to angular, and I am implementing the ng-file-upload directive. Can someone please show me how to clear the file input after the upload is successful?

I've tried a number of solutions online, and the only one I've managed to make it sort of working is with Jquery: input.replaceWith(input.val('').clone(true));

However, this only seem to clear it by appearance, I am still able to upload the supposedly cleared file if I try to submit again.

If someone could help me out that would be great.

2 Answers 2

1

It's pretty simple just do this.

var uploadInstance = $upload.upload({
     url: 'resources/upload',
     file: $files  // for multiple files
})
progress(function (evt) {
    //progress
})
.success(function (data) {
    //Success upload
})
.error(function (data) {
    //Error upload    
});

and when you want uploadInstance to be reset, just call

uploadInstance.abort(); 
Sign up to request clarification or add additional context in comments.

Comments

0

This is working for me, but it's not ideal solution:

HTML:

<form name="photosForm" ng-submit="upload(photos.file)">
  <input type="file"
         name="file"
         ngf-select
         ng-class="{'invalid': photosForm.file.$invalid}"
         ng-model="photos.file"
         required/>
  <button type="submit" ng-disabled="photosForm.$invalid"></button>
</form>

JS:

$scope.upload = function(file) {
  Upload(file).then(function() {
    $scope.photos = { file: undefined };
  }, function(err) {
    $scope.errGallery = err.data.message;
  });
}

CSS:

.invalid {
  color: red;
  border-color: red;
}

Now what this does for me is. It leaves name of the file inside file input, but it will not be valid file inside anymore, as there is no file, it is set to 'undefined'. This is good in a sense that submit button will be disabled and you can't upload twice the same file by mistake, also that 'invalid' css class will be applied as input is required but there is no file set and it will color my input border and text to red.

Comments

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.