2

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?

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

2 Answers 2

4

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

Replace backend_model with this :-

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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