1

I'm new to Magento, created a simple CRUD module, which have a name, description and logo columns, Name and description are saving to the DB without any problems, but I need help on how to upload an image. Here is my whole module file structure, and code of Save controller, Thanks everyone for helping me!

enter image description here

<?php
namespace Elogic\Vendors\Controller\Adminhtml\Vendors;
use Magento\Backend\App\Action;
class Save extends Action
{
 /**
   * @var \Elogic\Vendors\Model\Vendors
   */
 protected $_model;
 /**
   * @param Action\Context $context
   * @param \Elogic\Vendors\Model\Vendors $model
   */
 public function __construct(
 Action\Context $context,
 \Elogic\Vendors\Model\Vendors $model
 ) {
 parent::__construct($context);
 $this->_model = $model;
 }
 /**
   * {@inheritdoc}
   */
 protected function _isAllowed()
 {
 return $this->_authorization->isAllowed('Elogic_Vendors::vendors_save');
 }
 /**
   * Save action
   *
   * @return \Magento\Framework\Controller\ResultInterface
   */
 public function execute()
 {
 $data = $this->getRequest()->getPostValue();
 /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
 $resultRedirect = $this->resultRedirectFactory->create();
 if ($data) {
 /** @var \Elogic\Vendors\Model\Vendors $model */
 $model = $this->_model;
 $id = $this->getRequest()->getParam('id');
 if ($id) {
 $model->load($id);
 }
 $model->setData($data);
 $this->_eventManager->dispatch(
 'vendors_vendors_prepare_save',
 ['vendors' => $model, 'request' => $this->getRequest()]
 );
 try {
 $model->save();
 $this->messageManager->addSuccess(__('Vendor saved'));
 $this->_getSession()->setFormData(false);
 if ($this->getRequest()->getParam('back')) {
 return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]);
 }
 return $resultRedirect->setPath('*/*/');
 } catch (\Magento\Framework\Exception\LocalizedException $e) {
 $this->messageManager->addError($e->getMessage());
 } catch (\RuntimeException $e) {
 $this->messageManager->addError($e->getMessage());
 } catch (\Exception $e) {
 $this->messageManager->addException($e, __('Something went wrong while saving vendor'));
 }
 $this->_getSession()->setFormData($data);
 return $resultRedirect->setPath('*/*/edit', ['entity_id' => $this->getRequest()->getParam('id')]);
 }
 return $resultRedirect->setPath('*/*/');
 }
}
Muhammad Hasham
8,75211 gold badges53 silver badges105 bronze badges
asked Jan 31, 2019 at 13:04
0

1 Answer 1

1

Uploading the product image in Magento 2

<?php
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Backend\App\Action;
protected $_fileUploaderFactory;
public function __construct(
 \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
 Action\Context $context
) {
 $this->_fileUploaderFactory = $fileUploaderFactory;
 parent::__construct($context);
}
public function execute(){
 $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);
 $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
 $uploader->setAllowRenameFiles(false);
 $uploader->setFilesDispersion(false);
 $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)
 ->getAbsolutePath('images/');
 $uploader->save($path);
}

I hope this will help. If you still find any difficulty please let me know

answered Jan 31, 2019 at 13:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.