Restricting file type in Box Content Uploader
AnsweredHello,
Is there a way to restrict the file types allowed to be uploaded through the Box Content Uploader (React UI element)? I could only find the following posts about it and it doesnt seem like there is support for handling file restrictions:
https://support.box.com/hc/en-us/community/posts/360049138574-File-restriction-based-on-extensions
https://support.box.com/hc/en-us/community/posts/360052614673-Box-UI-Elements-Content-Uploader?input_string=Restricting%20file%20type%20in%20Box%20Content%20Uploader
https://github.com/box/box-ui-elements/issues/489
The first link is not very helpful as the linked article is about error codes. The second link, the user reports that there isn't a way to stop the upload event from happening based on the file types they're looking for. The third link they closed the issue out without stating any intention of implementing this feature into the content uploader
-
Official comment
Hello!
The best approaches are to:
- add the requestIntercepter to throw an error if a user tires to upload any file types you do not want to allow
- using the accept attribute on the input file field
Hope this helps!
Best,
Kourtney, Box Developer Advocate
Comment actions -
Hi Kourtney,
Thanks for the response! How would you go about using the accept attribute on the input file field? I don't see the Content Uploader accepting any properties related to extensions or an <input> element being used in the code.
-
For anyone who needs to restrict file extensions in the future, our solution was to parse the file extension from the filename located in config.data.name and if it was not one of our accepted extensions, we would null out the folder id the file is getting uploaded to (config.data.parent.id = null). There was a very tricky error with this though. Since the file upload to Box is chunked, the config object does not always contain the filename. So you must wrap your parsing of the extension in an if statement to check if the filename is present: if (!!config?.data?.name). If you don't do this, the upload will keep trying until it fails and nothing about this error will be logged to the console.
Post is closed for comments.
Comments
5 comments