[Vuejs]-I want to send img by POST

0👍

use this code:

const urlImage = "<your url image>"

async function uploadImageWithUrl() {
  const { buffer, ext } = await fetch(image).then(async res => {
    if (!res.ok) throw res

    const buffer = await res.arrayBuffer()
    const type = res.headers.get('content-type')
    if (!type.startsWith("image/")) throw new Error("Url response not is type image")

    const ext = type.split("/", 2)[1] ?? 'jpg'

    return { buffer, ext }
  })

  const form = new FormData() // form post image 
  form.append("image" /* is name param upload */, new File([buffer], `image.${ext}`))
  
  console.log(await fetch(<url post image>, {
    body: form,
    method: 'post'
  }).then(res => res.json()).catch(e => console.error(e)))
}

view demo – thank you imgur.com for their great service: https://playcode.io/1086025

Leave a comment