-
-
Save BigUncleYemi/61e6fec8b8600f88ae391c12eacaa471 to your computer and use it in GitHub Desktop.
FilePond Cloudinary
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 createCloudinary = (cloudName, unsignedUploadPreset) => ({ | |
process: (fieldName, file, metadata, load, error, progress, abort) => { | |
// `fieldName` and `meta` are not used for now | |
const url = `https://api.cloudinary.com/v1_1/${cloudName}/upload`; | |
const xhr = new XMLHttpRequest(); | |
const formData = new FormData(); | |
xhr.open('POST', url, true); | |
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); | |
xhr.upload.addEventListener('progress', e => { | |
progress(e.lengthComputable, e.loaded, e.total); | |
}); | |
xhr.onreadystatechange = e => { | |
if (xhr.readyState !== 4) { | |
return; | |
} | |
if (xhr.status >= 200 && xhr.status < 300) { | |
const response = JSON.parse(xhr.responseText); | |
load(response.public_id); | |
return; | |
} | |
error('oh no!'); | |
}; | |
formData.append('upload_preset', unsignedUploadPreset); | |
formData.append('tags', 'browser_upload'); | |
formData.append('file', file); | |
xhr.send(formData); | |
return { | |
abort: () => { | |
xhr.abort(); | |
} | |
} | |
}, | |
revert: null | |
}); | |
// create FilePond | |
const pondElement = document.querySelector('input'); | |
const pond = FilePond.create( | |
pondElement, { | |
server: createCloudinary('pqina', 'filepond_cloudinary_demo') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment