2

I'm developing an admin grid list. In the edit form i have an image field, but when i save it it does't appear in:

$data = $this->getRequest()->getPostValue();

and in:

$data = $this->getRequest();

I can see:

  protected 'postParams' => 
    object(Zend\Stdlib\Parameters)[73]
      private 'storage' (ArrayObject) => 
        array (size=9)
          'form_key' => string 'gSliqxNCIitA25WV' (length=16)
          'entity_id' => string '9' (length=1)
          'name' => string 'bla' (length=3)
          'surname' => string 'bla' (length=3)
          'email' => string 'bla' (length=3)
          'company' => string 'bla' (length=3)
          'note' => string 'bla' (length=3)
          'store_id' => string '1' (length=1)
          'url_key' => string 'bla' (length=3)
  protected 'fileParams' => 
    object(Zend\Stdlib\Parameters)[74]
      private 'storage' (ArrayObject) => 
        array (size=1)
          'image' => 
            array (size=5)
              ...
  protected 'version' => string '1.1' (length=3)
  protected 'headers' => 
    object(Zend\Http\Headers)[75]
      protected 'pluginClassLoader' => 
        object(Zend\Http\HeaderLoader)[78]
          protected 'plugins' => 
            array (size=55)
              ...

And maybe i can get the image from the 'fileParams', but i don't know how. I already tried: var_dump($this->getRequest()->getParams());

so, any idea?

4 Answers 4

2

This happens as your post data does not contain the information about the image file. It is stored in $_FILES.

So, Use Magento-2's Uploader To upload the file. Collect the post params in Request (lets say in $data ) and store the uploaded file name (from uploader result) in the $data under the same key as your field name in database.

Use this Modified $data for saving.

1

First set form in <form enctype="multipart/form-data"> and Check controller file in like

if (isset($_FILES['image']) && isset($_FILES['image']['name']) && strlen($_FILES['image']['name'])) {
 echo $_FILES['image']['name'];
}
0

I got the information for my issue.

I got that in $_FILES.

0

Try below code to get image data in controller.

$bgImage = $this->getRequest()->getFiles('bg_image'); // replace with Image field name
$fileName = ($bgImage && array_key_exists('name', $bgImage)) ? $bgImage['name'] : null;
            if ($bgImage && $fileName) {
                try {
                    $uploader = $this->_objectManager->create(
                        'Magento\MediaStorage\Model\File\Uploader',
                        ['fileId' => 'bg_image']
                    );

                $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);

                $imageAdapterFactory = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')
                    ->create();

                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(true);
                $uploader->setAllowCreateFolders(true);

                $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
                    ->getDirectoryRead(DirectoryList::MEDIA);

                $result = $uploader->save(
                    $mediaDirectory
                        ->getAbsolutePath()
                );
                $model->setBgImage($result['file']);
            } catch (\Exception $e) {
                if ($e->getCode() == 0) {
                    $this->messageManager->addError($e->getMessage());
                }
            }
        }

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.