Hook problem before uploading element upload component

problem description

1. Element+vue uploads pictures to Qiniuyun
2, uptoken is removed from the backend in before-upload hook
3, before-upload has not been returned during the first processing, and upload operation is performed. Upload error
4. Subsequent uploads can be uploaded normally using token,

.

related codes

<el-upload ref="upload" 
            action="//up.qbox.me/" 
            :show-file-list="false"
            :data="upToken" 
            :on-success="uploadSuccess"
            :before-upload="beforeUpload"                                                     
            accept="image/jpeg,image/gif,image/png,image/jpeg"
            style="display:inline">
 <el-button size="small"></el-button>
</el-upload>

// 
return {
    // token
    upToken: {}
}

// 
  // 
  // token
beforeUpload (file) {     
    axios({
      method: "get",
      url: "/get_qiniu_token",
    }).then(response => {
        this.upToken = {token: response.data.msg}
    })
},

uploadSuccess(response, file, fileList){
    console.log(file)
},

what result do you expect?

whether the token, can be obtained when uploading for the first time and then upload

Oct.10,2021

< H2 > implementation to write a promise for asynchronous processing < / H2 >
<el-upload
  class="avatar-uploader"
  action="https://upload.qbox.me"
  :data="dataObj" 
  :show-file-list="false"
  :on-success="handleAvatarSuccess"
  :before-upload="beforeAvatarUpload">
  <img v-if="imgTextForm.topPic" :src="imgTextForm.topPic" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
< hr >
beforeAvatarUpload(file) {
  const _self = this
  console.log(file)
  const isJPG = /(gif|jpg|jpeg|png|GIF|JPG|JPEG)$/.test(file.type)
  if (isJPG) {
    var params = {
      fileType: 0
    }
    return new Promise((resolve, reject) => {
      doGetQiniuToken(params).then(res => {
        if (res.status === 200) {
          var curr = moment(new Date()).valueOf()
          var prefix = moment(file.lastModified).format('HHmmss').toString()
          var suffix = Base64.encode(file.name)
          const key = encodeURI(`${curr}/${prefix}_${suffix}`)
          console.log(key)
          const token = res.data.uptoken
          // console.log(token)
          _self.dataObj.token = token
          _self.dataObj.key = key
        }
        resolve(true)
      }).catch(err => {
        console.log(err)
        reject(false)
      })
    })
  } else {
    Message.error('.gif,jpeg,jpg,png')
    return false
  }
},
The hook

beforeUpload should return a Boolean value, but you write an asynchronous function, and the operation to get token should be done in created.


idea 1:
feels that you don't need to put this request to get token in beforeUpload ().
can get token
created () {

when you start the component.
this.getToken();

}
so the asynchronous problem should be solved

idea 2: upload your file step by step after selecting the file and do not upload it immediately. Add an upload action button
to change your code.

<el-upload ref="upload" 
            action="//up.qbox.me/" 
            :show-file-list="false"
            :data="upToken" 
            :on-success="uploadSuccess"
            ref="upload"
            :auto-upload="false"
            :before-upload="beforeUpload"                                                     
            accept="image/jpeg,image/gif,image/png,image/jpeg"
            style="display:inline">
 <el-button size="small"></el-button>
 <el-buttonsize="small" @click="submitUpload"></el-button>
</el-upload>
method:{
    submitUpload() {
        
    //token
    axios({
      method: 'get',
      url: '/get_qiniu_token',
    }).then(response => {
        this.upToken = {token: response.data.msg}
        //.
        this.$refs.upload.submit();
    })
        
    },
}
Menu