element-ui upload组件 上传文件类型限制

<el-upload
    class="c-upload"
    ref="upload"
    :action="actions"
    :headers="myHeaders"
    :data="myData"
    :limit='limit'
    accept=".xls,.xlsx"
    :on-exceed="onExceed"
    :on-change="onChange"
    :on-success="onSuccess"
    :on-error="onError"
    :file-list="fileList"
    :before-upload="beforeUpload"
    :auto-upload="false">
    <el-button
      slot="trigger"
      size="small"
      type="primary"
   >选取文件</el-button>
   <div
       slot="tip"
       class="el-upload__tip"
    >只能上传xls/xlsx文件,且不超过一个</div>
</el-upload>

解决办法:
上传之前:before-upload="beforeUpload"再次判断文件类型

beforeUpload(file) {
     console.log(file)
     var testmsg=file.name.substring(file.name.lastIndexOf('.')+1)
     const extension = testmsg === 'xls'
     const extension2 = testmsg === 'xlsx'
     // const isLt2M = file.size / 1024 / 1024 < 10
    if(!extension && !extension2) {
        this.$message({
            message: '上传文件只能是 xls、xlsx格式!',
            type: 'warning'
        });
    }
    // if(!isLt2M) {
    //     this.$message({
    //         message: '上传文件大小不能超过 10MB!',
    //         type: 'warning'
    //     });
    // }
    // return extension || extension2 && isLt2M
    return extension || extension2
},

————————————————
转载声明:「为什么名字都被占用」的原创文章
原文链接:https://blog.csdn.net/qq_37588752/article/details/89637283

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容