0

I need do upload file which accepts XLS files only file size limit 5mb Angular 13?

HTML

<input name="file" type="file" (change)="onSelectFile($event)" required/> <button type ="file"> </button>

Ts file

onSelectFile(event:any){
const allowed_type = ['application/xslx']
if (this.file.size >5000 * 1024 && this.files.type !== allowed_type) {
this.warning = true;     
 } else {
 this.warning = false; 
   }
}

here I can only get warning msg on size limit not file type

I need both the validations happen.

also tried in this way:

 if (this.file.size >5000 * 1024) {
    this.warning = true;     
     } else if (this.files.type !== allowed_type){
     this.warning = true; 
    }else{
this.warning = false;
}

Here I should not allow more 5mb and file type should accept only xsl format.

rkamlekar
  • 11
  • 4
  • What is your question? – Pterrat Mar 16 '22 at 09:35
  • check [this](https://stackoverflow.com/questions/4234589/validation-of-file-extension-before-uploading-file) for file type and [this](https://stackoverflow.com/questions/33598129/how-to-check-maximum-upload-file-size-in-javascript-validation) for file size – fixedDrill Mar 16 '22 at 09:36
  • not sure file extension for xlsx, but change like this `const allowed_type = ['.xlsx']` – fixedDrill Mar 16 '22 at 09:38
  • @Pterrat I should accepts only .xls files and file size should be 5mb. please suggest how to make it possible. – rkamlekar Mar 16 '22 at 09:58

1 Answers1

0

If you want to upload a particular file format just add accept tag over there in the input tab.

<input name="file" type="file" (change)="onSelectFile($event)" accept=".xls" required/> 

in .ts file you can check for the selected file is .xls or not by using

if (fileObj.type == "application/xls")
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Jigar
  • 17
  • 4