前端vue2.0文件上传 带检验后缀名功能复制粘贴直接用

在这里插入图片描述
在这里插入图片描述

   <template>
  <el-upload
    class="upload-demo"
    ref="upload"
    action="#"
    :limit="1"
    :on-change="handsuccse"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :on-error="handerr"
    :file-list="fileList"
    :auto-upload="false"
  >
    <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
    <!-- <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件,且不超过500kb
            </div> -->
  </el-upload>
</template>

<script>
export default {
  data () {
    return {
      fileList: [],
      ruleForm: {
        file: ""
      }
    }
  },
  methods: {
    handsuccse (file, fileList) {
      var filetypes = [".doc", ".xls", ".pdf", ".docx", ".xlsx"]  //这个地方想加什么后缀名就写入什么后缀名
      console.log('file', file.raw) // 这个才是文件
      let type = file.raw.name.split(".")
      console.log(type[type.length - 1])
      let end = "." + type[type.length - 1]
      let find = filetypes.filter(item => {
        return item == end
      })
      console.log(find)
      if (find.length <= 0) {
        this.$message.error('请上传正确的文档格式')
        this.fileList = []
        return
      } else {
        this.ruleForm.file = file.raw
        this.$message.success('文件上传成功')

      }

    },
    handlePreview (file) {
      console.log(file)
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
      this.ruleForm.file = []

    },
    handerr (err, file, fileList) {

    },
  },



}
</script>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值