2

File upload in Magento 2 store configuration

<section id="custom_section" translate="label" type="text" sortOrder="301" showInDefault="1" showInWebsite="1" showInStore="0">
 <label>Custom</label>
 <tab>sales</tab>
 <resource>Magento_Sales::config_sales</resource>
 <group id="custom_group" translate="label" type="text" sortOrder="6" showInDefault="1" showInWebsite="1" >
 <label>Security</label>
 <field id="custom_file_upload" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="6" showInDefault="1" showInWebsite="1" >
 <label>Upload custom file</label>
 <backend_model>Magento\Config\Model\Config\Backend\File</backend_model>
 <upload_dir config="system" scope_info="1">test</upload_dir>
 </field>
 </group>
</section>

This gives below upload option in Magento admin custom module configuration and the file is also getting uploaded at specified path.

enter image description here

  • Just like we import tablerates.csv which also inserts data into shipping_tablerate, how do I make sure that the file content also gets inserted into custom table?
asked Oct 31, 2018 at 18:36
1
  • Did you get any solution for this? Commented Dec 17, 2018 at 9:58

1 Answer 1

1

I got solution for this. So sharing here:

You have to extend backend model file in your custom module like below:

  1. In system.xml, update code as below for input type="file"
 <field id="customfile" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
 <label>Custom file</label>
 <comment></comment>
 <backend_model>Vendor\Package\Model\Config\Backend\CustomFileType</backend_model>
 <upload_dir>upload</upload_dir>
</field>
  1. In your custom module, Create file CustomFileType.php at below place:

Vendor\Package\Model\Config\Backend

  1. Override beforeSave method in that file as below:
<?php
namespace Vendor\Package\Model\Config\Backend;
class CustomFileType extends \Magento\Config\Model\Config\Backend\File
{
 public function beforeSave()
 {
 $value = $this->getValue();
 $file = $this->getFileData();
 if (!empty($file)) {
 /*** Here you can write your custom code to save data in custom table ***/
 $uploadDir = $this->_getUploadDir();
 try {
 /** @var Uploader $uploader */
 $uploader = $this->_uploaderFactory->create(['fileId' => $file]);
 $uploader->setAllowedExtensions($this->_getAllowedExtensions());
 $uploader->setAllowRenameFiles(true);
 $uploader->addValidateCallback('size', $this, 'validateMaxSize');
 $result = $uploader->save($uploadDir);
 } catch (\Exception $e) {
 throw new \Magento\Framework\Exception\LocalizedException(__('%1', $e->getMessage()));
 }
 $filename = $result['file'];
 if ($filename) {
 if ($this->_addWhetherScopeInfo()) {
 $filename = $this->_prependScopeInfo($filename);
 }
 $this->setValue($filename);
 }
 } else {
 if (is_array($value) && !empty($value['delete'])) {
 $this->setValue('');
 } elseif (is_array($value) && !empty($value['value'])) {
 $this->setValue($value['value']);
 } else {
 $this->unsValue();
 }
 }
 return $this;
 }
}
?>
answered Jun 20, 2019 at 13:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.