2

I create a Product View Tab and fill this with some PDF download links. Now link in Magento 1.9 I create a controller to prepare the download response.

I don't find any solution to download a file from VAR folder.

In Magento 1.9 I simply call the function $this->_prepareDownloadResponse( ... )

How I do this in Magento 2?

Thanks

1 Answer 1

7

For this \Magento\Framework\App\Response\Http\FileFactory is your friend.

You can look into this class for more reference

\Magento\OfflineShipping\Controller\Adminhtml\System\Config\ExportTablerates

 public function execute()
    {
        $fileName = 'tablerates.csv';
        /** @var $gridBlock \Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid */
        $gridBlock = $this->_view->getLayout()->createBlock(
            'Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid'
        );
        $website = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'));
        if ($this->getRequest()->getParam('conditionName')) {
            $conditionName = $this->getRequest()->getParam('conditionName');
        } else {
            $conditionName = $website->getConfig('carriers/tablerate/condition_name');
        }
        $gridBlock->setWebsiteId($website->getId())->setConditionName($conditionName);
        $content = $gridBlock->getCsvFile();
        return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
    }

Now, I think you can easily get an Idea how to crack your requirement :)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.