Skip to content

Upload csv file from a custom admin form issue

This is my fieldset from the uicomponent form:

<fieldset name="image_form_fieldset">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Form</item>
        </item>
    </argument>
    <field name="file">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">File</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="formElement" xsi:type="string">fileUploader</item>
                <item name="source" xsi:type="string">file</item>
                <item name="sortOrder" xsi:type="string">15</item>
                <item name="uploaderConfig" xsi:type="array">
                    <item name="url" xsi:type="url" path="regenerate/image/file"/>
                </item>
            </item>
        </argument>
    </field>
</fieldset>

This is my controller for the regenerate/image/file part:

use MagentoBackendAppActionContext;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkFilesystem;
use MagentoMediaStorageModelFileUploaderFactory;
use MagentoStoreModelStoreManagerInterface;
use MagentoFrameworkUrlInterface;
use MagentoBackendAppAction;

class File extends Action {

    /**
     * @var UploaderFactory
     */
    protected $uploaderFactory;

    /**
     * @var FilesystemDirectoryWriteInterface
     */
    protected $mediaDirectory;

    /**
     * @var StoreManagerInterface
     */
    protected $storeManager;

    public function __construct(
        Context $context,
        UploaderFactory $uploaderFactory,
        Filesystem $filesystem,
        StoreManagerInterface $storeManager
    )
    {
        parent::__construct($context);
        $this->uploaderFactory = $uploaderFactory;
        $this->mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
        $this->storeManager = $storeManager;
    }

    public function execute()
    {
        $jsonResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        try {
            $fileUploader = $this->uploaderFactory->create(['fileId' => 'file']);
            $fileUploader->setAllowedExtensions(['csv']);
            $fileUploader->setAllowRenameFiles(true);
            $fileUploader->setAllowCreateFolders(true);
            $fileUploader->setFilesDispersion(false);
            $fileUploader->validateFile();
            $result = $fileUploader->save($this->mediaDirectory->getAbsolutePath('tmp/imageUploader/images'));
            $result['url'] = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . 'regenerate_images/';
            return $jsonResult->setData($result);
        } catch (LocalizedException $e) {
            return $jsonResult->setData(['errorcode' => 0, 'error' => $e->getMessage()]);
        } catch (Exception $e) {
            error_log($e->getMessage());
            error_log($e->getTraceAsString());
            return $jsonResult->setData(['errorcode' => 0, 'error' => __('An error occurred, please try again later.'. $e->getMessage())]);
        }
    }
}

My code is failing at this part: $fileUploader = $this->uploaderFactory->create(['fileId' => 'file']);. Getting this error message:

An error occurred, please try again later.Notice: Trying to access array offset on value of type null in ../vendor/magento/framework/File/Uploader.php on line 226

Any idea why ? and how can I fix this. I just want to upload a csv file in the media folder.

Thanks