How to download the files in magento
Asked Answered
R

3

5

I uploaded some files for each customer in magento....

Then i listed the customers details with the uploaded file name ..

I need to download the file using magento code

This is the code:

public function downloadAction() {
        $entityid = $this->getRequest()->getParam('entity_id');
        $customer_data = Mage::getModel('customer/customer')->load($entityid);
        $filename = '';
        if($customer_data){
            $filename = $customer_data->getFileuploadname();
        }
        $filepath = '../uploads/'.$filename;

        if (! is_file ( $filepath ) || ! is_readable ( $filepath )) {
            throw new Exception ( );
        }
        $this->getResponse ()
                    ->setHttpResponseCode ( 200 )
                    ->setHeader ( 'Pragma', 'public', true )
                    ->setHeader ( 'Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true )
                    ->setHeader ( 'Content-type', 'application/force-download' )
                    ->setHeader ( 'Content-Length', filesize($filepath) )
                    ->setHeader ('Content-Disposition', 'inline' . '; filename=' . basename($filepath) );
        $this->getResponse ()->clearBody ();
        $this->getResponse ()->sendHeaders ();
        readfile ( $filepath );
        //exit(0);
    }

But it didsplays errors something like:

Trace:
#0 D:\wamp\www\mysite\app\code\core\Mage\Core\Controller\Varien\Action.php(419): Managecustomers_Users_IndexController->downloadAction()
#1 D:\wamp\www\mysite\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('download')
#2 D:\wamp\www\mysite\app\code\core\Mage\Core\Controller\Varien\Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#3 D:\wamp\www\mysite\app\code\core\Mage\Core\Model\App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#4 D:\wamp\www\mysite\app\Mage.php(683): Mage_Core_Model_App->run(Array)
#5 D:\wamp\www\mysite\index.php(87): Mage::run('', 'store')
#6 {main}

The uploads folder is in magento root folder...

How can i download the file....

The $filename have the filename uploaded that is coming from database...

EDIT :

When i removed the code:

 if (! is_file ( $filepath ) || ! is_readable ( $filepath )) {
            throw new Exception ( );
        }

Then changed the filepath as :

$filepath = 'http://localhost/mysite/uploads/'.$filename;

Then downloading done perfectly....

Radiancy answered 12/3, 2013 at 4:47 Comment(0)
R
10

This is the solution for this type of problems:

 public function downloadAction() {
        $entityid = $this->getRequest()->getParam('entity_id');
        $customer_data = Mage::getModel('customer/customer')->load($entityid);
        $filename = '';
        if($customer_data){
            $filename = $customer_data->getFileuploadname();
        }
        $filepath = Mage::getBaseDir('base').'/uploads/'.$filename;

        if (! is_file ( $filepath ) || ! is_readable ( $filepath )) {
            throw new Exception ( );
        }
        $this->getResponse ()
                    ->setHttpResponseCode ( 200 )
                    ->setHeader ( 'Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true )
                     ->setHeader ( 'Pragma', 'public', true )
                    ->setHeader ( 'Content-type', 'application/force-download' )
                    ->setHeader ( 'Content-Length', filesize($filepath) )
                    ->setHeader ('Content-Disposition', 'attachment' . '; filename=' . basename($filepath) );
        $this->getResponse ()->clearBody ();
        $this->getResponse ()->sendHeaders ();
        readfile ( $filepath );
        exit;
    }

The problem based on the file path issues.....now its solved....

Radiancy answered 12/3, 2013 at 5:33 Comment(3)
It is prefered to use _prepareDownloadResponse() as suggested by @Katabolism as it does not need exitRedd
I am trying to implement the same solution but it is giving me error as 'Call to a member function setHttpResponseCode() on a non-object' can you please let me know is there any other class which needs to include in file? Thank you.Postfix
Also tried to use _prepareDownloadResponse but not working.Postfix
K
12

What about use Magento code ? ... _prepareDownloadResponse()

 public function downloadAction()
    {
       $filename = '';
       if($customer_data){
          $filename = $customer_data->getFileuploadname();
       }
       $filepath = Mage::getBaseDir('base').'/uploads/'.$filename;

        if ($filename) {
            try {
                $this->_prepareDownloadResponse($filename, array('type' => 'filename', 'value' => $filepath));

            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
        } else {
            $this->_getSession()->addError($filepath . ' not found');
            $this->_redirect('adminhtml/cache');
            return;
        }
    }
Katabolism answered 9/10, 2015 at 11:30 Comment(3)
This should be definitely the accepted answer. Thanks @KatabolismRedd
Is it possible to download multiple files with _prepareDownloadResponse ?Vola
Is it possible to download multiple files with _prepareDownloadResponse ?Fiden
R
10

This is the solution for this type of problems:

 public function downloadAction() {
        $entityid = $this->getRequest()->getParam('entity_id');
        $customer_data = Mage::getModel('customer/customer')->load($entityid);
        $filename = '';
        if($customer_data){
            $filename = $customer_data->getFileuploadname();
        }
        $filepath = Mage::getBaseDir('base').'/uploads/'.$filename;

        if (! is_file ( $filepath ) || ! is_readable ( $filepath )) {
            throw new Exception ( );
        }
        $this->getResponse ()
                    ->setHttpResponseCode ( 200 )
                    ->setHeader ( 'Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true )
                     ->setHeader ( 'Pragma', 'public', true )
                    ->setHeader ( 'Content-type', 'application/force-download' )
                    ->setHeader ( 'Content-Length', filesize($filepath) )
                    ->setHeader ('Content-Disposition', 'attachment' . '; filename=' . basename($filepath) );
        $this->getResponse ()->clearBody ();
        $this->getResponse ()->sendHeaders ();
        readfile ( $filepath );
        exit;
    }

The problem based on the file path issues.....now its solved....

Radiancy answered 12/3, 2013 at 5:33 Comment(3)
It is prefered to use _prepareDownloadResponse() as suggested by @Katabolism as it does not need exitRedd
I am trying to implement the same solution but it is giving me error as 'Call to a member function setHttpResponseCode() on a non-object' can you please let me know is there any other class which needs to include in file? Thank you.Postfix
Also tried to use _prepareDownloadResponse but not working.Postfix
H
0

To use Magento's _prepareDownloadResponse() in a controller action when you have access to the file path do the following in your controller class:

public function downloadAction()
{
    $filePath = '/this/is/an/filepath';

    return $this->_prepareDownloadResponse(basename($filePath), file_get_contents($filePath));
}
Hemihedral answered 22/10, 2018 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.