1 <el-upload 2 class="c-upload" 3 ref="upload" 4 :action="actions" 5 :headers="myHeaders" 6 :data="myData" 7 :limit='limit' 8 accept=".xls,.xlsx" 9 :on-exceed="onExceed" 10 :on-change="onChange" 11 :on-success="onSuccess" 12 :on-error="onError" 13 :file-list="fileList" 14 :before-upload="beforeUpload" 15 :auto-upload="false" 16 > 17 <el-button 18 slot="trigger" 19 size="small" 20 type="primary" 21 >選取文件</el-button> 22 <div 23 slot="tip" 24 class="el-upload__tip" 25 >只能上傳xls/xlsx文件,且不超過一個</div> 26 </el-upload>
解決辦法:
上傳之前:before-upload="beforeUpload"再次判斷文件類型

1 beforeUpload(file) { 2 console.log(file) 3 var testmsg=file.name.substring(file.name.lastIndexOf('.')+1) 4 const extension = testmsg === 'xls' 5 const extension2 = testmsg === 'xlsx' 6 // const isLt2M = file.size / 1024 / 1024 < 10 7 if(!extension && !extension2) { 8 this.$message({ 9 message: '上傳文件只能是 xls、xlsx格式!', 10 type: 'warning' 11 }); 12 } 13 // if(!isLt2M) { 14 // this.$message({ 15 // message: '上傳文件大小不能超過 10MB!', 16 // type: 'warning' 17 // }); 18 // } 19 // return extension || extension2 && isLt2M 20 return extension || extension2 21 }, 22 ———————————————— 23 版權聲明:本文為CSDN博主「為什么名字都被占用」的原創文章,遵循CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。 24 原文鏈接:https://blog.csdn.net/qq_37588752/article/details/89637283