Created
June 30, 2022 15:24
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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