How to get file type before uploading via the Upload Widget
We are using the same Upload Widget code to upload both images and videos. We need to set different upload presets depending on if the file is an image vs. video. Is there a simple way of obtaining the file type before it gets uploaded so that we can set the proper preset?
Right now, we are doing something pretty hacky with `prepareUploadParams()` and `callback()`:
useCloudinaryUpload({ ...
uploadPreset: '', clientAllowedFormats: ['image', 'video'], prepareUploadParams: (cb, params, data) => { params = [].concat(params) let preset
if (fileType) { if (/image/.test(fileType)) { preset = 'image_upload' } if (/video/.test(fileType)) { preset = 'video_upload' } } else { throw new Error() } cb({ ...params, uploadPreset: preset, }) }, callback: (uploadError, result) => { if (result.event === 'upload-added') { fileType = result?.info?.file?.type } if (result.event === 'source-changed') { source = result.info.source } }, })
1
-
Hi Jason,
Thanks for reaching out.
There is actually no way on our side to know the resource_type before you upload the file. Usually what you can also do is either check the extension of the file or its mime-type and then assign the right resource_type.
Hope that helps.
Best,
Loic
1
Post is closed for comments.
Comments
1 comment