Today we are going to explain how to download the file programmatically in Magento 2.
Here we have added controller through which we can download image based on the click or call directly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
namespace Magemonkeys\Download\Controller\Index; use Magento\Framework\App\Filesystem\DirectoryList; class Image extends \Magento\Framework\App\Action\Action { protected $fileFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\App\Response\Http\FileFactory $fileFactory ) { $this->fileFactory = $fileFactory; parent::__construct($context); } public function execute() { $mediafilePath = 'media/download/homeimage.jpg'; $downloadFilenm = 'homeimage.jpg'; $imagearr['type'] = 'mediafilePath'; $imagearr['value'] = $filePath; $imagearr['rm'] = 0; return $this->fileFactory->create($downloadFilenm, $imagearr, DirectoryList::PUB); } } |
We can add our logic in execute() method which will help us to download homeimage.jpg by calling this action.
[crayon-63d3e90fd4cd9230514157/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.