Skip to content

Instantly share code, notes, and snippets.

@coreyward
Created June 30, 2022 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreyward/503c578bba35d674036cbbe87b72e87e to your computer and use it in GitHub Desktop.
Save coreyward/503c578bba35d674036cbbe87b72e87e to your computer and use it in GitHub Desktop.
Quick reference code for uploading an image to the Sanity Studio from the browser
const getImageData = async url => {
const response = await global.fetch(url)
const contentType = response.headers.get("Content-Type")
return response.blob().then(imageData => ({ contentType, imageData }))
}
const uploadImage = ({ imageData, contentType }) =>
client.assets.upload("image", imageData, { contentType })
// usage example
const imageData = await getImageData(someUrl)
const image = await uploadImage(imageData)
console.log(image._id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment