通过api接口把图片上传给后端(踩坑)

1.加multipart/form-data

//用户端图片上传  加multipart/form-data
export const uploadImage = (params) => {
  return $.post('/app/pat/pushpull/imgUpload', params, { headers: { "Content-Type": "multipart/form-data" } })
}

 2.还有最主要用 new FormData 来传。。。。

 async getImageByHost (file, field) {
      console.log(111, file)
      let File = new FormData()
      File.append("file", file)
      File.append("type", "Copy")
      
      const [err, res] = await to(uploadImage(File))
      if (err) return this.$toast(err.errorMsg)
      console.log(2222, res)
      if (!res) {
        this[field] = []
      }
      return res
    }

 

//上传前通用限制 
function beforeUpload(file: any) {
    // 限制图片 格式、size、分辨率
    const isJPG = file.type === "image/jpeg"
    const isJPEG = file.type === "image/jpeg"
    const isGIF = file.type === "image/gif"
    const isPNG = file.type === "image/png"
    const isLt2M = file.size / 1024 / 1024 < 2
    if (!(isJPG || isJPEG || isPNG)) {
      Modal.error({
        title: "只能上传JPG、JPEG、PNG格式的图片~",
      })
    } else if (!isLt2M) {
      Modal.error({
        title: "图片超过2M限制,不允许上传~",
      })
    }
    return (isJPG || isJPEG || isPNG) && isLt2M

  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值