上传文件应该只接受XLS文件大小限制5mbAngular 13



我需要上传只接受XLS文件的文件,文件大小限制为5mb Angular 13?

HTML

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

Ts文件

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; 
}
}

在这里,我只能得到关于大小限制的警告消息,而不是文件类型

我需要进行两次验证。

也尝试过这种方式:

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

这里我不应该允许更多的5mb,文件类型应该只接受xsl格式。

如果您想上传特定的文件格式,只需在输入选项卡中添加accept标记即可。

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

在.ts文件中,您可以使用检查所选文件是否为.xls

if (fileObj.type == "application/xls")

最新更新