Box UI Elements - Content Uploader
Hi Team,
I want to validate the file type before upload complete. I have added a listener 'beforeupload' where i could retrieve the added files before upload, but i was not able to cancel the event in case of invalid file type. Check the code as below.
uploader.on('beforeupload', (data) => {
if (data != undefined) {
var fileName = data[0].name;
var ext = (-1 !== fileName.indexOf('.')) ? fileName.replace(/.*[.]/, '').toLowerCase() : '';
var allowed = ['doc', 'docx', 'xls', 'xlsx', 'pdf'];
if (!allowed.length) { return true; }
for (var i = 0; i < allowed.length; i++) {
if (allowed[i].toLowerCase() == ext) { return true; }
}
alert('This file types are not allowed to upload!');
return false;
}
});
in case of invalid file type, the file should not be added on the upload widget.
Please sign in to leave a comment.
Comments
0 comments