Clear input type file if invalid

Asked by At

I'm using angularjs and this library to upload files. When a file is selected, I call a function. When a file's size is greater than the limit I try to clear the input field in the function.

But when I submit and a file with accepted file size is not uploaded after trying to upload a file greater than the limit, the form is invalid. It is not a required field either. I tried to set null to the field and also $setPristine(). It did not work.

The function that's called when a file is selected:

$scope.uploadFiles = function (file, inpId, form) {
debugger

if(form && form.attachBillnm.$invalid){

  console.log($(inpId), form);
  $(inpId).val(null);
  form.attachBillnm.$setPristine()
  console.log($(inpId), form);
  return
}

The html:

<input type="file" id="addBillId" 
    ngf-select="uploadFiles($files, '#addBillId', expenseForm)" 
    ngf-max-size="'4 MB'"
       accept=".pdf, image/*" name="attachBillnm" ng-model="expn.billUpload">
    <span ng-show="expenseForm.attachBillnm.$invalid && expenseAdd" style="color: #a94442;">
    Max size for a file: <b>4MB</b> </span>

0 Answers