el-upload传多余的参数

本文介绍了在Vue应用中使用ElementUI的el-upload组件进行图片上传,包括成功回调函数的处理、文件类型检查以及与后端API的交互。
摘要由CSDN通过智能技术生成

1,html

<el-upload

          :data="extraData"

          class="avatar-uploader"

          :action="$actionUrl + '/upload/single/img'"

          :show-file-list="false"

          :on-success="handleAvatarSuccess"

          :before-upload="beforeAvatarUpload"

          @change="handUpImg($event)"

        >

          <img v-if="imageUrl" :src="imageUrl" class="avatar" />

          <div v-else class="avatar1">

            <img src="../../../../assets/img/shangchuan.png" alt="" />

          </div>

          <div class="el-upload__tip" slot="tip" style="margin-left: 30px">

            {{ imageUrl ? "重新上传" : "请上传png格式图片" }}

          </div>

        </el-upload>

 

 2,js

    handleAvatarSuccess(res, file) {

      this.imageUrl1 = res.data;

      this.imageUrl = URL.createObjectURL(file.raw);

      //   const _ = this;

      //   let files = file.raw;

      //   const formData = new FormData();

      //   formData.append("file", files);

      //   formData.append("prefix", "user");

      //   _.$api.files_api

      //     .singleImg(formData, {

      //       headers: {

      //         "Content-Type": "multipart/form-data",

      //         Authorization: localStorage.getItem("token"),

      //       },

      //     })

      //     .then((res) => {

      //       if (res.code == 200) {

      //         this.imageUrl1 = res.data;

      //       }

      //     })

      //     .catch(() => {});

    },

    beforeAvatarUpload(file) {

      const isJPG = file.type === "image/jpeg";

      const isPNG = file.type === "image/png";

      if (!isJPG && !isPNG) {

        return this.$message.error("上传头像图片只能是 JPG 或 PNG 格式!");

      }

3,data

 

 data() {

    return {

      extraData: {

        prefix: "user",

      },

    };

  },

  • 37
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值