How to Add File Upload Control in Magento 2 Admin Configuration
Magento 2 admin supports multiple input types such as text box, radio button, dropdown, multi select boxes etc., the values of which stored and displayed in different forms of grids, forms, fields, images, etc.
Sometimes, while developing Magento 2 extensions, you may require to allow the admin to upload multiple files like icon image to show in the frontend or CSV with some zip codes to use the values somewhere in the extension. There are multiple file types such as PHP, XML, JPG, DOC, XLS, CSV and to allow the admin to upload files, you require to add file upload control in Magento 2 admin configuration.
However, the default Magento 2 does not have the facility to upload files for admin configuration. Thus, I have come up with the programmatic solution to add file upload control in Magento 2 admin configuration.
Programmatic Solution to Add File Upload Control in Magento 2 Admin Configuration
Add the below code in system.xml file at Meetanshi/Extension/etc/adminhtml/
1 2 3 4 5 6 |
<field id="custom_file_id" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>File Upload Button</label> <backend_model>Magento\Config\Model\Config\Backend\File</backend_model> <upload_dir config="system">MyUploadDir</upload_dir> </field> |
Your file will be uploaded at magentoRoot/pub/media/MyUploadDir/filename.
That’s the only thing you need to do to add file upload control in admin configuration. You can also add custom image upload control in Magento 2 backend to allow only image file type uploads in stores configuration.
If you have queries, feel free to ask in the Comment section below.
I would be happy to solve your queries.
Do not forget to share the post with Magento Community via social media.
Thank you.
Related: How to Allow File Attachment in WYSIWYG in Magento 2
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
Prev
How to Restore Coupon Code after Order Cancellation in Magento 2
How to Add Custom Menu Item in Magento 2 Frontend
Next