element-plus以form-data提交图片

html如下

vue,elementplus axios提交图片

        <div v-if="!inputState" style="display:flex" class="right-data">
            <img v-if="imageUrl" :src="imageUrl" style="width:130px;height:130px">
            <el-upload
              class="avatar-uploader"
              action="#"
              :show-file-list="false"
              :auto-upload="false"
              :on-change="fileChange"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </div>
        <div v-else>
          <img v-if="param.userAvator" :src="imgUrl+param.userAvator" style="width:130px;height:130px">
        </div>

js


    //头像事件
    handleAvatarSuccess(res, file) {   //上传成功
            console.log(res,file,'change头像123')
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    fileChange (file, fileList) {     //改变,上传,成功都会调用
      console.log(file,fileList,'change头像')
      this.imageUrl = URL.createObjectURL(file.raw)
      if (fileList.length > 0) {
        this.file = file.raw
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }

提交

const formData = new FormData()
//判断是否更换头像
if(this.param.region !== this.upData.region)
  formData.append('region',this.param.region)
//发送请求
try {axios}catch{}

还有就是要忘记headers:

export function addtUserData (data) {
  return request({
    url: api +'你的地址',
    method: 'post',
    headers: {'Content-Type': 'multipart/form-data'},
    data
  })
}

自己刚入门,望指教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值