{"id":1690,"date":"2021-04-07T09:56:51","date_gmt":"2021-04-07T09:56:51","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/create-custom-download-link-in-magento-2\/"},"modified":"2025-07-17T09:24:22","modified_gmt":"2025-07-17T03:54:22","slug":"create-custom-download-link-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-custom-download-link-in-magento-2\/","title":{"rendered":"How to Create Custom Download Link in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 CMS is a widely suited platform for various types of online businesses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, default\u00a0Magento 2\u00a0allows importing-exporting data on the admin side by selecting \u2018Entity Type\u2019 from System > Data Transfer > Import. It offers one link named \u201cDownload Sample File.\u201d It automatically downloads a CSV file when you click on that link.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, what if you want to&nbsp;<strong><em>create custom download link in Magento 2<\/em><\/strong>&nbsp;to allow your customers to download some sample data file or any document from the frontend?&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In all such cases, you can use the below solution:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Create Custom Download Link in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code in<strong>&nbsp;Index.php<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Meetanshi\/Extension\/Controller\/Download<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendore\\Extension\\Controller\\Download;\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\App\\Response\\Http\\FileFactory;\nuse Magento\\Framework\\Exception\\FileSystemException;\nuse Magento\\Framework\\Filesystem\\DirectoryList;\nuse Psr\\Log\\LoggerInterface;\n\nclass Index extends Action\n{\n    protected $downloader;\n    protected $logger;\n    protected $directory;\n\n    public function __construct(Context $context,\n                                FileFactory $fileFactory,\n                                LoggerInterface $logger,\n                                DirectoryList $directory)\n    {\n        $this->logger = $logger;\n        $this->downloader = $fileFactory;\n        $this->directory = $directory;\n        parent::__construct($context);\n    }\n\n    public function execute()\n    {\n\n        $fileName = $this->getRequest()->getParam('file');\n        $filePath = '';\n\n        try {\n            $filePath = $this->directory->getPath(\"media\") . '\/Dir_path\/' . $fileName;\n        } catch (FileSystemException $e) {\n            $this->logger->info($e->getMessage());\n        }\n        try {\n            return $this->downloader->create($fileName, [\n                'type' => 'filename',\n                'value' => $filePath,\n            ],\n                \\Magento\\Framework\\App\\Filesystem\\DirectoryList::MEDIA,\n                'application\/octet-stream');\n        } catch (\\Exception $e) {\n            $this->logger->info($e->getMessage());\n        }\n        \/\/  return true;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use the above code wherever you want to add a custom download link in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do share the post with Magento 2 store owners via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1690","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1690"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1690\/revisions"}],"predecessor-version":[{"id":18142,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1690\/revisions\/18142"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}