Question

I am following below Article to upload file in store configuration: https://www.mageplaza.com/devdocs/how-file-upload-sytem-configuration-magento-2.html

I have defined custom backend model as mentioned in this artice to validate my file type:

<?php

namespace Vendor\Package\Model\Config\Backend;

class CustomFileType extends \Magento\Config\Model\Config\Backend\File
{
    /**
     * @return string[]
     */
    public function getAllowedExtensions() {
        return ['csv'];
    }
}
?>

and in system.xml, it's like this:

<field id="import" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Import CSV</label>
                    <comment>Comment</comment>
                    <backend_model>Vendor\Package\Model\Config\Backend\CustomFileType</backend_model>
                    <upload_dir>upload</upload_dir>
</field>

but it's not working. I can upload image files too!!

What's wrong with this code?

Was it helpful?

Solution

I found answer by referring

vendor\magento\module-config\Model\Config\Backend\File.php

Correct Function name is _getAllowedExtensions not getAllowedExtensions

So, correct method is:

public function _getAllowedExtensions() {
        return ['csv'];
    }

OTHER TIPS

Replace backend_model with this :-

<backend_model>Magento\Config\Model\Config\Backend\File</backend_model>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top