Last active
February 9, 2024 16:15
-
-
Save rikschennink/7f1896f6b52e4730e24156fdec2525e6 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
Hello, How can I use this to upload large file videos with more than 100 MB