cancel
Showing results for 
Search instead for 
Did you mean: 

How to add File upload in the Gift Message form

How to add File upload in the Gift Message form

I am trying to Add image upload in the Gift Message. To achieve , created a Field: image_url in the Table: gift message using db_schema.xml and used the extension_attributes.xml to set and retrieve the value.

I have added the file upload element in the gift-message-form.html. But unable to pass the uploading file information to gift Message API model (Because gift message using /carts/mine/gift-message API).

Can anyone explain with an example how to upload the file using the controller with Ajax and store the uploaded file name in the Gift message table along with this record.

Thanks in Advance

2 REPLIES 2

Re: How to add File upload in the Gift Message form

Hello @Musammil 

 

So you need to upload the product image in Magento 2 from controller file? Here are snippet:

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);
 
}

Kindly refer this code and add file upload functionality for product or giftcard.

Problem solved? Click Accept as Solution!

Re: How to add File upload in the Gift Message form

Hi Banu Periwal,

Thanks for your reply. Actually I have added the file upload element in the
gift-mesage-form.html file and would like to know how to pass this value to
the file upload controller to upload the file in the specific folder then
I need to pass the uploaded file url to the gift-message table.

When I go through the code In Gift-message Form, they called the
gift-message-API on the Update Button in the cart page.

Note : Exactly, I need to Ajax call from the gift-message form to the
controller to upload the file and pass the file url to gift-message API.

Once again thanks for your valuable reply.