2
<?php

namespace Modulename\Merchant\Controller\Adminhtml\Document;

class Filedownload extends \Modulename\Merchant\Controller\Adminhtml\Document
{
protected $_downloader;

private $logger;

protected $_directory;

protected $_documentFactory;

public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Registry $coreRegistry,
    \Adolmedia\Merchant\Model\DocumentFactory $documentFactory,
    \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
    \Magento\Framework\Filesystem\DirectoryList $directory,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
    $this->_downloader = $fileFactory;
    $this->logger = $logger;
    $this->_documentFactory = $documentFactory;
    $this->_directory = $directory;
    $this->resultPageFactory = $resultPageFactory;
    parent::__construct($context, $coreRegistry);
}

public function execute()
{
    $documentId = $this->getRequest()->getParam('document_id');

//        try {
        $document = $this->_documentFactory->create()->load($documentId);

        $filePath = $document->getFilePath();

        $fileName = $document->getFileName();

        $file = $this->_directory->getPath("media") . $filePath . $fileName; // do your validations /** * do file download */
        return $this->_downloader->create($fileName, @file_get_contents($file));
 //        }
 //        catch (\Exception $e) {
//            $this->logger->critical($e->getMessage());
//        }
}
}

when I click download link it shows in browser.

The web page at http://127.0.0.1/magento23/admin/merchant/document/filedownload/document_id/2/key/8800ab485374f4bb24103af1e0472723f66314f9f2914d220fe3ba69738ad509/ might be temporarily down or it may have moved permanently to a new web address.

How can I solve this issue??

  • Have you created Document.php at \Modulename\Merchant\Controller\Adminhtml path ? if not then first you should extend your class with \Magento\Backend\App\Action Apart from this in constructor, parent constructor should be called with only context .... Put a die inside execute method and check if it works or not. – Yash Shah Feb 15 '19 at 4:32
  • yes i have already created parent class.. tested using echo statement .. only this return $this->_downloader->create($fileName, @file_get_contents($file)); causing error – Gomathi Feb 15 '19 at 11:07
  • Have suggested an answer. Try that and let me know if that works. – Yash Shah Feb 15 '19 at 11:36
1

Try something like below.

$content_type = 'application/zip';
$this->_downloader->create(
    $fileName,
    [
        'type' => 'filename',
        'value' => $filePath . $fileName,
        'rm' => false
    ],
    \Magento\Framework\App\Filesystem\DirectoryList::ROOT,
    $content_type
);

Give appropriate content_type for desired filetype and in below line 'value' => $filePath . $fileName

Make sure the $filepath is absolute filepath like /var/www/html/magento/pub/media

This should work.

| improve this answer | |
  • Prefect answer... thank u so much ... yash7690 – Gomathi Feb 15 '19 at 11:46
  • You're welcome. Please upvote and accept answer so that it help others as well. – Yash Shah Feb 15 '19 at 11:51
  • $file = $this->_directory->getPath("media") . $filePath . $fileName; // do your validations /** * do file download */ $content_type = 'application/pdf'; return $this->_downloader->create( $fileName, [ 'type' => 'filename', 'value' => $file, 'rm' => false ], \Magento\Framework\App\Filesystem\DirectoryList::ROOT, $content_type ); need to call \Magento\Framework\Filesystem\DirectoryList $directory to get directory path – Gomathi Feb 15 '19 at 12:43
  • Ok thats perfect :) – Yash Shah Feb 15 '19 at 12:47
0

You have to pass two more parameters in factory intialization:

$this->_downloader->create(
$fileName, 
@file_get_contents($file),
'base dir of the file to download here',
'application/octet-stream'//content type
);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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