Element-ui upload 手动上传文件

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 = [];
        }
      })
    },

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值