七牛、阿里云oss文件上传前需要先获取key时利用element ui的上传插件操作方法


<el-form-item label="Excel文件" :label-width="formLabelWidth" prop="excelurl">
              <el-upload class="avatar-uploader"
                ref="upload"
                action="//uploadrul/"
                :auto-upload="false"   
                :show-file-list="false"
                :on-success="handleFileSuccess"
                :on-error="handleFileError"
                :on-change="handleChange"
                :before-upload="beforeFileUpload"
                :data="uploadFile">
                <el-button slot="trigger" 
                size="small" type="primary" 
                class="file-select-btn">选择文件</el-button>
                <el-button class="upload-file-btn" size="small" 
                type="primary" 
                :disabled="isSuccessUpload" 
                @click="uploadAction">上传</el-button>
              </el-upload>
              <el-input type="text" v-model="form.excelurl" class="upload-file-input" 
              v-if="fileState" disabled></el-input>
              <el-button  size="small" type="primary" class="change-file-btn" 
              v-if="btnState" @click="fileState=false;btnState=false">修改</el-button>
      </el-form-item>

其中: auto-upload=“false” 为选择文件后不立即上传 on-change=“handleChange” 为状态改变立即触发 js关键是利用this.$refs.upload.submit()手动触发上传
Js代码:

     beforeFileUpload(file) {
        let filetype = file.name.slice(file.name.lastIndexOf('.'),file.name.length)
        if (filetype === '.xls' || filetype === '.xlsx') {
          // return true
        } else {
          this.$message.error('请上传EXCEL格式文件')
          return false
        }
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传文件大小不能超过 2MB!')
          return  false;
        }        
      },
      handleChange(file){
        if(!this.fileState){
          this.$http.post(GET_UPLOAD_TOKEN, {}).then(res => {
            if (res.data.ret === 0) {
              this.uploadFile = {key:file.name,token : res.data.data.upToken}
              this.form.excelurl = file.name
              this.fileState = true
            }else{
              return false
            }
          })
        }
      },
      uploadAction(){
        this.$refs.upload.submit()
      },
            ```


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值