I need to create a image upload field in my Ui form.
I have created below files.
But while trying to upload an image, it shows a below error.
Please suggest me a solution to save a image in DB.
I got following error
Upload.php
<?php
namespace OX\HomeSlider\Controller\Adminhtml\HomeSlider\Image;
use Magento\Framework\Controller\ResultFactory;
/**
* Class Upload
*/
class Upload extends \Magento\Backend\App\Action
{
/**
* Image uploader
*
* @var \Namespace\Module\Model\ImageUploader
*/
protected $imageUploader;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Catalog\Model\ImageUploader $imageUploader
*/
public function __construct(
\Magento\Backend\App\Action\Context $context, \Magento\Catalog\Model\ImageUploader $imageUploader
)
{
parent::__construct($context);
$this->imageUploader = $imageUploader;
}
/**
* Check admin permissions for this controller
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('OX_HomeSlider::HomeSlider');
}
/**
* Upload file controller action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
try {
$result = $this->imageUploader->saveFileToTmpDir('image');
} catch (\Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
di.xml
<virtualType name="OXHomeSliderImageUploader" type="Magento\Catalog\Model\ImageUploader">
<arguments>
<argument name="baseTmpPath" xsi:type="string">homeslider/tmp</argument>
<argument name="basePath" xsi:type="string">homeslider</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="OX\HomeSlider\Controller\Adminhtml\HomeSlider\Image\Upload">
<arguments>
<argument name="imageUploader" xsi:type="object">OXHomeSliderImageUploader</argument>
</arguments>
</type>
ox_homeslider_form.xml
<field name="image" formElement="fileUploader">
<settings>
<notice translate="true">Allowed file types: jpeg, gif, png.</notice>
<label translate="true">Image</label>
<componentType>fileUploader</componentType>
</settings>
<formElements>
<fileUploader>
<settings>
<allowedExtensions>jpg jpeg gif png</allowedExtensions>
<maxFileSize>2097152</maxFileSize>
<uploaderConfig>
<param xsi:type="string" name="url">ox_homeslider/homeslider_image/upload</param>
</uploaderConfig>
</settings>
</fileUploader>
</formElements>
</field>
save.php
namespace OX\HomeSlider\Controller\Adminhtml\Post;
class Save extends \Magento\Backend\App\Action
{
public function __construct(
\Magento\Backend\App\Action\Context $context, \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
)
{
$this->resultRedirectFactory = $resultRedirectFactory;
parent::__construct($context);
}
public function execute()
{
$id = $this->getRequest()->getParam('id');
$postModel = $this->_objectManager->create('OX\HomeSlider\Model\Post');
if ($id) {
$postModel = $postModel->load($id);
}
$post = $this->getRequest()->getParams();
if(empty($post['id'])) {
$post['id'] = null;
}
$postModel->setData($post);
$postModel->save();
return $this->resultRedirectFactory->create()->setPath('homeslider/post/index');
return $result;
}
}
Best Answer
it is giving error because of this code
Reason is the mentioned url
theme/design_config_fileUploader/save
may not exist or having wrong code.you can refer category edit form and uploaded action for this, below are the path for files
Category Form
Upload Action
Check this answer to create controller action to upload image
Update:
change url in your above
<uploaderConfig>
node to belowCreate a controller action at path Filename -
Upload.php
In this File add below code
Add below code to your module's
etc/di.xml
Note: Do not forgot to change Namespace and Module to your Namespace and Module name