How to Create Custom Download Link in Magento 2
Magento 2 CMS is a widely suited platform for various types of online businesses.
Owing to the features it offers and capabilities of customization, the CMS is popular among the store owners. Today, I will talk about one such feature that can be developed using the below code and how a business owner can use it.
For example, default Magento 2 allows importing-exporting data on the admin side by selecting ‘Entity Type’ from System > Data Transfer > Import. It offers one link named “Download Sample File.” It automatically downloads a CSV file when you click on that link.
Now, what if you want to create custom download link in Magento 2 to allow your customers to download some sample data file or any document from the frontend?
You can allow the visitors to download a privacy policy or product user guide or any custom file that has information they need to know.
In all such cases, you can use the below solution:
Method to Create Custom Download Link in Magento 2
Use the below code in Index.php at app/code/Meetanshi/Extension/Controller/Download
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php namespace Vendore\Extension\Controller\Download; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Response\Http\FileFactory; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Filesystem\DirectoryList; use Psr\Log\LoggerInterface; class Index extends Action { protected $downloader; protected $logger; protected $directory; public function __construct(Context $context, FileFactory $fileFactory, LoggerInterface $logger, DirectoryList $directory) { $this->logger = $logger; $this->downloader = $fileFactory; $this->directory = $directory; parent::__construct($context); } public function execute() { $fileName = $this->getRequest()->getParam('file'); $filePath = ''; try { $filePath = $this->directory->getPath("media") . '/Dir_path/' . $fileName; } catch (FileSystemException $e) { $this->logger->info($e->getMessage()); } try { return $this->downloader->create($fileName, [ 'type' => 'filename', 'value' => $filePath, ], \Magento\Framework\App\Filesystem\DirectoryList::MEDIA, 'application/octet-stream'); } catch (\Exception $e) { $this->logger->info($e->getMessage()); } // return true; } } |
Use the above code wherever you want to add a custom download link in Magento 2.
That’s all.
Have any doubts about creating custom download link in Magento 2? If yes, use the Comments section below for me to help you out.
Also, do share the post with Magento 2 store owners via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Hi, I’m eager to use this code, but I’m a beginner and have hesitation to implement the code in my magento2 website. …. Do you have any tutoring service? Thank you!
Hey,
Please contact us if you have any queries regarding coding implementation: https://meetanshi.com/contacts
Hey, nice post.
Hello,
Thank you