Element-ui upload 手动上传文件

墨寂弦
2023-12-01

element 手动上传

(随手一发,可以拿过来就用,不做解释看不懂的可以照搬,代码可复制)

 

<div>
 <el-upload
   class="upload"
   ref="upload"
   action="string"
   :file-list="fileList"
   :auto-upload="false"
   :http-request="uploadFile"
   :on-change="handleChange"
   :on-preview="handlePreview"
   :on-remove="handleRemove"
   multiple="multiple"
 >
  <el-button
   slot="trigger"
   size="small"
   @click="delFile"
   class="chaxuns fontSizes"
  >选取文件</el-button>
  <div class="upfile">
   <el-button
     style="margin-left: 10px;"
     size="small"
     type="success"
     class="chaxuns fontSizes"
     @click="submitUpload"
   >上传文件</el-button>
  </div>
 </el-upload>          
</div>
    // 选取文件
    delFile() {
      this.fileList = [];
    },
    handleChange(file, fileList) {
      this.fileList = fileList;
    },
    uploadFile(file) {
      this.formData.append("file", file.file);
    },
    submitUpload() {
      const formData = new FormData();
      formData.append("file", this.fileList[0].raw);
      // ↓后端请求接口↓
      upload(formData).then(res => {
        if (res.data.length === 0) {
          this.$message.warning("上传失败")
          this.fileList = [];
        } else {
          this.$message.success("上传文件成功")
          this.fileList = [];
        }
      })
    },

 

 类似资料: