element 手动上传覆盖原来的方法

该文章展示了如何使用Vue.js的el-upload组件实现Excel文件的上传,包括限制文件大小、检查文件类型、自定义上传逻辑以及使用axios处理HTTP请求。在onChange事件中进行文件验证,而在http-request属性中覆盖默认上传行为,进行自定义的文件提交。
摘要由CSDN通过智能技术生成
 <el-upload
          ref="myupload"
          class="upload-demo"
          drag
          :fileList="fileList"
          :action="actionPath"
          :headers="headers"
          :on-error="fileError"
          :on-progress="onProgress"
          :before-upload="beforeUpload"
          :http-request="toUpload"
          :show-file-list="false"
          accept=".xls,.xlsx"
          :on-change="onChange"
          :auto-upload="false"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">拖拽文件至此处或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip" style="margin-bottom: 12px">
            支持xls、xlsx表格的excel表格,大小不超过100MB
          </div>
        </el-upload>
通过http-request 实现
:http-request="toUpload"
onChange 判断文件  并获取需要展示的数据
onChange(file) {
      // this.uploadLoading = true
      if (this.allowType) {
        const isLt2M = file.size / 1024 / 1024 > 100
        if (isLt2M) {
          this.$message.warning('文件大小不能超过 100MB!')
          this.uploadLoading = false
          return false
        }
        var index = this.allowType.indexOf(file.name.substring(file.name.lastIndexOf('.')).toLowerCase())
        if (index < 0) {
          this.$message({
            message: '文件格式不正确',
            type: 'warning'
          })
          this.uploadLoading = false
          return false
        }
      }
      this.fileList = [file]
      
      this.fileInfoParams = [
        {
          name: file.name,
          size: file.size
        }
      ]
    },
手动点击上传
clickConfirmImport(){
 this.$refs.myupload.submit()  // 会执行toUpload()
}

 toUpload(param) {
      // 覆盖原本的上传
      console.log(param)
      const formData = new FormData()
      formData.append('file', param.file)
      axios({
        url: param.action,   // 上传的action
        method: 'post',
        data: formData,
        headers: { 'X-Token': getToken() }  //项目token
      }).then((res) => {
        console.log(res)
      })
    },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值