1

I want to add the file uploaded in custom option (adminend).

In CustomOption modifier:

return [
        'arguments' => [
            'data' => [
                'config' => [
                    'label' => __('Image Uploader'),
                    'componentType' => 'fileUploader',
                    'formElement' => 'fileUploader',
                    'component' => 'Magento_Ui/js/form/element/file-uploader',
                    'elementTmpl' => 'Magento_Ui/components/file-uploader',
                    'dataScope' => 'file',
                    'fileInputName' => 'image',
                    'uploaderConfig' => [
                        'url' => $this->urlBuilder->addSessionParam()->getUrl(
                            'product/upload/image'
                        ),
                    ],

                    'sortOrder' => $sortOrder,
                ],
                [
                    'elementTmpl' => 'ui/dynamic-rows/cells/thumbnail',
]
            ],
        ]
    ];

In Image Controller

public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Catalog\Model\ImageUploader $imageUploader
    ) {
        parent::__construct($context);
        $this->imageUploader = $imageUploader;
    }

    /**
     * Upload file controller action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $imageId = $this->_request->getParam(image);
        try {
            $result = $this->imageUploader->saveFileToTmpDir($imageId);
        } catch (\Exception $e) {
            $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
        }
        return $this->resultFactory->create(ResultFactory::TYPE_JSON)
            ->setData($result);
    }

In di.xml

<virtualType name="XXX\YYY\ImageUpload" type="Magento\Catalog\Model\ImageUploader">
<arguments>
    <argument name="baseTmpPath" xsi:type="string">option/images</argument>
    <argument name="basePath" xsi:type="string">option</argument>
    <argument name="allowedExtensions" xsi:type="array">
        <item name="jpg" xsi:type="string">jpg</item>
        <item name="jpeg" xsi:type="string">jpeg</item>
        <item name="gif" xsi:type="string">gif</item>
        <item name="png" xsi:type="string">png</item>
    </argument>
</arguments>
</virtualType>
<type name="XXX\YYY\Controller\Adminhtml\Upload\Image">
<arguments>
    <argument name="imageUploader" xsi:type="object">XXX\YYY\ImageUpload</argument>
</arguments>
</type>

But not working.. It throws

Invalid parameter given. A valid $_FILES[] identifier is expected.

Thanks

2
  • I have faced the following error while saving - Notice: Array to string conversion in /var/www/html/m233/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3105 If you have any solution then please let me know.
    – MK Patel
    Commented Feb 27, 2020 at 12:32
  • @MKPatel any solution above issue becuse our case same issue Commented Apr 11 at 9:35

2 Answers 2

1
      $imageId = $this->_request->getParam(image);

Need to use

         $files = $this->getRequest()->getFiles(); 
1
  • it expect second parameter expect to be string but object given.
    – Ali Raza
    Commented Sep 14, 2020 at 11:47
0

You can give a try to below, It should work.

    // Magento\MediaStorage\Model\File\UploaderFactory
    $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'yourFieldName']);
    $uploaderFactory->setAllowedExtensions(['jpg', 'jpeg', 'png', 'pdf']);
    // Magento\Framework\Image\AdapterFactory
    $imageAdapter = $this->adapterFactory->create();
    $uploaderFactory->setAllowRenameFiles(true);
    $uploaderFactory->setFilesDispersion(true);
    // Magento\Framework\Filesystem
    $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
    $destinationPath = $mediaDirectory->getAbsolutePath('customer');
    $result = $uploaderFactory->save($destinationPath);

    $image = $result['yourFieldName'];

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.