Skip to main content

How to Upload image on Frontend inusing custom module?

Source Link
sudo55
  • 1k
  • 2
  • 16
  • 36

How to Upload image Frontend in custom module

I am trying to upload an image in the frontend. my controller is

public function execute()
{
    /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    if (!$this->formKeyValidator->validate($this->getRequest())) {
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }

    $data = $this->reviewSession->getFormData(true);
    if ($data) {
        $rating = [];
        if (isset($data['ratings']) && is_array($data['ratings'])) {
            $rating = $data['ratings'];
        }
    } else {
        $data = $this->getRequest()->getPostValue();
        $rating = $this->getRequest()->getParam('ratings', []);
    }
    if (($product = $this->initProduct()) && !empty($data)) {
        /** @var \Magento\Review\Model\Review $review */
        $pathurl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'review_module/';
        $mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
        $mediapath = $this->_mediaBaseDirectory = rtrim($mediaDir, '/');
        $files = $this->getRequest()->getFiles();
        if (isset($data['image']))
        {
          $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);
          $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
          $uploader->setAllowRenameFiles(true);
          $path = $mediapath . '/review_module/';
          $data['image'] = $files['image']['name'];
            $result = $uploader->save($path);
        }
        $review = $this->reviewFactory->create()->setData($data);
        $review->unsetData('review_id');

        $validate = $review->validate();
        if ($validate === true) {
            try {
                $review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
                    ->setEntityPkValue($product->getId())
                    ->setStatusId(Review::STATUS_PENDING)
                    ->setCustomerId($this->customerSession->getCustomerId())
                    ->setStoreId($this->storeManager->getStore()->getId())
                    ->setStores([$this->storeManager->getStore()->getId()])
                    ->save();

                foreach ($rating as $ratingId => $optionId) {
                    $this->ratingFactory->create()
                        ->setRatingId($ratingId)
                        ->setReviewId($review->getId())
                        ->setCustomerId($this->customerSession->getCustomerId())
                        ->addOptionVote($optionId, $product->getId());
                }

                $review->aggregate();
                $this->messageManager->addSuccess(__('You submitted your review for moderation.'));
            } catch (\Exception $e) {
                $this->reviewSession->setFormData($data);
                $this->messageManager->addError(__('We can\'t post your review right now.'));
            }
        } else {
            $this->reviewSession->setFormData($data);
            if (is_array($validate)) {
                foreach ($validate as $errorMessage) {
                    $this->messageManager->addError($errorMessage);
                }
            } else {
                $this->messageManager->addError(__('We can\'t post your review right now.'));
            }
        }
    }
    $redirectUrl = $this->reviewSession->getRedirectUrl(true);
    $resultRedirect->setUrl($redirectUrl ?: $this->_redirect->getRedirectUrl());
    return $resultRedirect;
}

i am getting this error enter image description here