0

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?

  • Code looks fine. Did you run di:compile and flushed the cache as well? – Sukumar Gorai Jun 20 at 11:47
  • yes. I flushed the cache. do i need to run di:compile in developer mode too? – Khushbu Jun 20 at 11:50
  • Yes run once because if not compiled then might be an issue. – Sukumar Gorai Jun 20 at 11:56
  • Thank you @SukumarGorai. But i got the solution. See my answer. – Khushbu Jun 20 at 11:57
  • Yes I have checked. Happy coding! – Sukumar Gorai Jun 20 at 11:58
2

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'];
    }
  • its not necessary – Rk Rathod Jun 20 at 12:17
  • You can try this code! – Khushbu Jun 20 at 12:25
  • i'm also try this – Rk Rathod Jun 20 at 12:27
0

Replace backend_model with this :-

<backend_model>Magento\Config\Model\Config\Backend\File</backend_model>
  • this is not working! – Khushbu Jun 20 at 11:49
  • i think your controller is not called – Rk Rathod Jun 20 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.