The on-error hook function can be used to display the error message returned by the back end when the upload fails when processing the el-upload component

/ / HTML area<el-upload class="upload-demo" ref="upload" :headers="headers" :action="excelUploadApi"
:on-change="handleChange" :on-error="handleError" :show-file-list="false">
	<el-button slot="trigger" class="filter-item" type="warning" size="small" 
	icon="el-icon-upload">The import button</el-button>
</el-upload>// Use :on-error="handleError" hook functionCopy the code

However, the format of err returned after printing is not easy to obtain. I searched the Internet and found two kinds of great god’s solutions, which are recorded here for future encounters

Plan a

Parse () converts err. Message directly through json.parse () instead of converting err

handleError(err, file, fileList) { // Upload failed hook function
    console.log('err', err)
    console.log('err'.JSON.parse(err.message))
    if (file.status == 'fail') {
            this.$message.error(JSON.parse(err.message).message)
    }
}
Copy the code
Scheme 2

Convert the Error message to a string, then remove the “Error:”, and what’s left is a JSON, then converted to an object

handleError(err, file, fileList) { // Upload failed hook function
    console.log('err', err)
    let myError=err.toString();// Turn to string
    myError=myError.replace("Error: "."") // Remove the previous "Error:"
    myError=JSON.parse(myError);/ / object
    console.log(myError);
}
Copy the code

In the part used in my project, the manual upload process shows the success message after the upload is successful

Note that the on-success section here just writes the method name and does not take parameters<el-form-item  label="file" :label-width="formLabelWidth">
  <el-upload
    class="upload-demo"
    drag
    ref="upload"
    action="http://127.0.0.1:5000/upload_user_dict_excel"
    :auto-upload="false"
    :on-success="success"
    :data="{'language':form.language,'type':form.type}">
    <i class="el-icon-upload"></i>
    <div class="el-upload__text">Drag the file here, or<em>Click on the upload</em></div>
  </el-upload>
</el-form-item>

<el-button type="primary" @click="submitDilog('form')">determine</el-button>
Copy the code

methods

submitDilog(formName){
  if(this.add_type){
    let data=new FormData()
    data.append("language".this.form.language)
    data.append("type".this.form.type)
    data.append("error".this.form.error)
    data.append("suggestions".this.form.suggestions)
    data.append("info".this.form.info)
    axios.post("http://127.0.0.1:5000/upload_user_dict",data).then(res= >{
      alert(res.data.message)
    })
    this.dialogFormVisible = false
    this.form.error="".this.form.suggestions=' '.this.form.info= ' '.this.form.type='Spelling custom'.this.form.language='English'
  }else{
        // Manually commit
        this.$refs.upload.submit()
        this.dialogFormVisible = false}}success(response,file,fileList){
  alert(response.message)
}
Copy the code