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.
- Just like we import
tablerates.csv
which also inserts data intoshipping_tablerate
, how do I make sure that the file content also gets inserted into custom table?
asked Oct 31, 2018 at 18:36
Slimshadddyyy
1,3575 gold badges39 silver badges58 bronze badges
-
Did you get any solution for this?Pribhav– Pribhav2018年12月17日 09:58:15 +00:00Commented Dec 17, 2018 at 9:58
1 Answer 1
I got solution for this. So sharing here:
You have to extend backend model file in your custom module like below:
- 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>
- In your custom module, Create file CustomFileType.php at below place:
Vendor\Package\Model\Config\Backend
- 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;
}
}
?>
default