Skip to content

Instantly share code, notes, and snippets.

@jefflau
Created February 28, 2019 05:19
Show Gist options
  • Save jefflau/e79fc48127bbf19df7cbd58d7990daab to your computer and use it in GitHub Desktop.
Save jefflau/e79fc48127bbf19df7cbd58d7990daab to your computer and use it in GitHub Desktop.
Cloudinary unsigned upload
const cloudName = 'xxxxxx'
const cloudinaryUrl = `https://api.cloudinary.com/v1_1/${cloudName}/upload`
const unsignedUploadPreset = 'xxxxxx'
export function upload(file) {
const body = new FormData()
body.append('file', file)
body.append('upload_preset', unsignedUploadPreset)
return (
fetch(cloudinaryUrl, {
method: 'POST',
body
})
.then(res => res.json()) // json-ify the readable strem
.then(body => {
// use the https:// url given by cloudinary; or eager property if using transformations
const imageUrl = body.eager ? body.eager : body.secure_url
// set the uploading status to false
return imageUrl
})
// eslint-disable-next-line no-console
.catch(err => console.log('err', err))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment