Upload 上传文件 设置请求参数

本文介绍了如何在表单中正确设置content-type为form-data,重点讲解了图片上传的步骤,包括使用Vue组件和FormData对象进行文件上传,以及处理上传前后状态和错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

不同的数据类型需要设置不同的 content-type 在表单中进行上传的时候则需要设置 content-type:form-data

  • 比方说上传图片:
<el-upload
   class="avatar-uploader"
   action=""
   accept="image/jpeg, image/png"
   :auto-upload="false"
   :show-file-list="false"
   :file-list="addDetaile.qrcodeImg"
   :on-change="uploadSectionFile"
  >
   <el-button slot="trigger" size="medium" type="primary"
    >上传</el-button
   >
   <el-button
     type="danger"
     size="medium"
     @click="removeImg('eq')"
     >删除</el-button
    >
    <p>建议上传200*200的图片</p>
 </el-upload>

    uploadSectionFile(file) {
      this.uploadImg(file, "eq");
    },
    uploadImg(file, status) {
      if (status == "tx") {
        this.addDetaile.coverImg = [file];
      } else {
        this.addDetaile.qrcodeImg = [file];
      }

      let param = new FormData();
      // 上传文件对象 名称file与后台控制器参数要一致
      param.append("file", file.raw);
      this.$http({
        headers: { "Content-Type": "multipart/form-data" },
        url: this.$http.adornUrl("/file/upload"),
        method: "post",
        data: param,
      }).then(({ data }) => {
        if (data && data.code === 200) {
          if (status == "tx") {
            this.coverImg = data.data; // 图片上传回显
          } else {
            this.qrcodeImg = data.data; // 图片上传回显
          }
        } else {
          this.$message({
            message: data.message,
            type: "error",
            duration: 1500,
          });
        }
      });
    },
    removeImg(str) {
      if (str == "tx") {
        this.coverImg = "";
      } else {
        this.qrcodeImg = "";
      }
    },
  • 上传表格数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值