Allowed File Extensions

3 posts by 2 authors in: Forums > CMS Builder
Last Post: October 31, 2019   (RSS)

By gversion - October 24, 2019

Hello,

I am creating an upload field for a form. I would like to be able to use a JavaScript function that checks the allowed extensions.

Currently I am hardcoding the allowed file types:

	if(fs>0){
		var types = ['application/vnd.ms-excel','application/msword','application/pdf','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
		for(var i=0;i<fs;i++){
	  	if(types.toString().indexOf(files[i].type) == -1) {
	  		error += 'Disallowed file type\n';
	  	}
		}
	}

I would like to improve this by allowing the file types defined in cmsb section editor area but I am not sure how to access this array.

Can someone please advise?

Thank you,

Greg

By daniel - October 30, 2019

Hi Greg,

To answer your specific question: you can use the function getSchemaFields($sectionName) to return an array of all the fields in that section, then you can search through the returned array to look for the upload field in question. The "allowedExtensions" property of that field should contain the extensions configured in the section editor.

However, note that this will be a list of fileĀ extensions which is different from the mime-type required by your JS snippet. Some possible solutions are to modify your JS to check file extension rather than mime-type or find a list of extensions<->mime-type that could be used to map the correct values.

If you're using CMSB functions to perform the upload, also note that there's a danger in checking for mime-type on the front end and extension on the back end (current CMSB behaviour), as they are generally related but fundamentally different things. This can lead to false positives/negatives in edge cases. This is why JS file-type checking is disabled for the default CMSB uploader, as our current library is only able to check for mime-type and not the extension.

Let me know if you have any questions, or if I can help with anything else!

Thanks,

Daniel
Technical Lead
interactivetools.com