3

I have created a form with Ui component. It contains a file field. When I upload a file it shows an error in the console enter image description here

Here is my code Ui component form

         <field name="ffl">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="source" xsi:type="string">ffl</item>
                    <item name="label" xsi:type="string" translate="true">Licence Pdf</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="formElement" xsi:type="string">fileUploader</item>
                    <item name="required" xsi:type="boolean">true</item>
                    <item name="uploaderConfig" xsi:type="array">
                        <item name="url" xsi:type="url" path="ffl/ffl/fflupload"/>
                    </item>
                </item>
            </argument>
        </field>    

Fileupload controller

namespace NameSpace\Ffl\Controller\Adminhtml\Ffl;

use Magento\MediaStorage\Model\File\UploaderFactory;
use Magento\Framework\Controller\ResultFactory;

class Fflupload extends \NameSpace\Ffl\Controller\Adminhtml\Ffl
{    
    private $uploaderFactory;
    private $mediaDirectory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Registry $registry,       
        \Magento\Framework\Filesystem $filesystem,
        UploaderFactory $uploaderFactory
    ) {
        $this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
        $this->uploaderFactory = $uploaderFactory;  
        parent::__construct($context, $registry);
    }

    /**
     * Save action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        try {
            $data = $this->getRequest()->getPostValue();
            $uploader = $this->uploaderFactory->create(['fileId' => 'ffl']);
            $uploader->setAllowRenameFiles(true);
            $uploader->setAllowedExtensions(['doc','pdf']);
            $path = $this->mediaDirectory->getAbsolutePath('fflfiles/');        ;
            $result = $uploader->save($path);

            $result['cookie'] = [
                'name' => $this->_getSession()->getName(),
                'value' => $this->_getSession()->getSessionId(),
                'lifetime' => $this->_getSession()->getCookieLifetime(),
                'path' => $this->_getSession()->getCookiePath(),
                'domain' => $this->_getSession()->getCookieDomain(),
            ];
         } catch (\Exception $e) {
             $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
         }
        return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);           
    }
}

Please help me.

  • Please check my ans. – Aasim Goriya Dec 22 '18 at 12:36
3

Please replace your UI Component with following code.

Change imageUploader instead of fileUploader in your formElement.

       <field name="ffl">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="source" xsi:type="string">ffl</item>
                    <item name="label" xsi:type="string" translate="true">Licence Pdf</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <!-- - Remove fileUploader -->
                    <!-- + Add imageUploader -->
                    <item name="formElement" xsi:type="string">imageUploader</item>
                    <item name="required" xsi:type="boolean">true</item>
                    <item name="uploaderConfig" xsi:type="array">
                        <item name="url" xsi:type="url" path="ffl/ffl/fflupload"/>
                    </item>
                </item>
            </argument>
        </field>    
  • Thank you, It works. Is it possible to remove the "Select From Gallery" button? i.prntscr.com/9QBrEPg-QGuypJaV_sL39A.png – Jancy Abraham Dec 24 '18 at 10:19
  • Did you get a solution? How to remove "Select From Gallery" Button? – Magento User Jan 9 at 8:47
  • It's not worked for me in Magento 2.3. I'm getting error: "Exception #0 (BadMethodCallException): Missing required argument $baseTmpPath" Can you please help me out? – Dhara Bhatti May 20 at 12:41
0

Other vendor(moduel-catalog , module-email , module-ui) don't override when it required then after override

To test, I first made the edit only in the file 'vendor/magento/module-theme/view/adminhtml/ui_component/design_config_form.xml',

find formElement="fileUploader" and replace with formElement="imageUploader"

and it worked!

Seems to be a bug, which will be patched in Magento 2.3.1 and 2.3.0 release.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.