0

I want to create a file uploader using Ui form, upload and process it in adminpanel.

this is my Ui form code

        <field name="fileUploader" formElement="fileUploader">
            <settings>
                <notice translate="true">Allowed file types: csv, xls, xlsx.</notice>
                <label translate="true">File</label>
                <componentType>fileUploader</componentType>
            </settings>
            <formElements>
                <fileUploader>
                    <settings>
                        <allowedExtensions>csv xls xlsx</allowedExtensions>
                        <maxFileSize>10000000</maxFileSize>
                        <placeholderType>File</placeholderType>
                        <uploaderConfig>
                            <param xsi:type="string" name="url">szk_customreports/bankdifference/savemellat</param>
                        </uploaderConfig>
                    </settings>
                </fileUploader>
            </formElements>
        </field>

everything is ok but my save controller is not working and i'm getting 404 error. xdebug showing that it's passing data to controller correctly but execute() is not running. this is my controller :

namespace SZK\CustomReports\Controller\Adminhtml\BankDifference;


use Magento\Backend\App\Action;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\File\Csv;
use Magento\Framework\File\UploaderFactory;
use Magento\Framework\Filesystem;
use Magento\Framework\Message\ManagerInterface;
use Netweb\Mellat\Api\Data\GridInterface;
use Netweb\Mellat\Model\ResourceModel\Grid\Grid\CollectionFactory as MellatCollection;
use Netweb\Mellat\Model\ResourceModel\Grid as MellatResourceModel;
use Netweb\Shamsi\Helper\Data;
//use SZK\CustomReports\Model\ResourceModel\BankDifference as BankResourceModel;
//use SZK\CustomReports\Model\ResourceModel\BankDifference\CollectionFactory as BankCollection;
use SZK\CustomReports\Model\BankDifferenceFactory as BankFactory;


class SaveMellat extends Action implements HttpGetActionInterface
{
    protected $messageManager;
    protected $filesystem;
    protected $fileUploader;
    protected $csvProcessor;
    protected $mellatCollection;
    protected $mellatResourceModel;
    protected $bankFactory;

    public function __construct(
        Action\Context $context,
        ManagerInterface $messageManager,
        Filesystem $filesystem,
        UploaderFactory $uploaderFactory,
        Csv $csvProcessor,
        MellatCollection $collectionFactory,
        MellatResourceModel $resourceModel,
//        BankResourceModel $bankResource,
//        BankCollection $bankCollection
        BankFactory $bankDifferenceFactory
    )
    {
        parent::__construct($context);
        $this->messageManager = $messageManager;
        $this->filesystem = $filesystem;
        $this->fileUploader = $uploaderFactory;
        $this->csvProcessor = $csvProcessor;
        $this->mellatCollection = $collectionFactory;
        $this->mellatResourceModel = $resourceModel;
        $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
//        $this->bankResourceModel = $bankResource;
//        $this->bankCollection = $bankCollection;
        $this->bankFactory = $bankDifferenceFactory;
        $this->execute();
    }

    public function execute(): string
    {
        $file = $_FILES['docFile'];

        $this->readCsvFile($file);


    }

this is my router:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="szk_customreports" frontName="szk_customreports">
            <module name="SZK_CustomReports"/>
        </route>
    </router>
</config>


enter image description here

1
  • Make sure you flush your cache after creating a new controller, routes are cached.
    – David Tay
    May 31, 2021 at 13:45

1 Answer 1

0

If you created new controller then please run below command

php bin/magento setup:di:compile

it will generate depenedency class in generated folder.

hope this helps you :)

11

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.