Created
February 28, 2019 05:19
-
-
Save jefflau/e79fc48127bbf19df7cbd58d7990daab to your computer and use it in GitHub Desktop.
Cloudinary unsigned upload
This file contains 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 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