如何在magento中下载文件

如何在magento中下载文件,magento,download,magento-1.7,Magento,Download,Magento 1.7,我在magento为每个客户上传了一些文件 然后我用上传的文件名列出了客户的详细信息 我需要使用magento代码下载该文件 代码如下: public function downloadAction() { $entityid = $this->getRequest()->getParam('entity_id'); $customer_data = Mage::getModel('customer/customer')->load($entit

我在magento为每个客户上传了一些文件

然后我用上传的文件名列出了客户的详细信息

我需要使用magento代码下载该文件

代码如下:

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);
    }
 if (! is_file ( $filepath ) || ! is_readable ( $filepath )) {
            throw new Exception ( );
        }
但它确实显示了一些错误,比如:

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}
上载的
文件夹位于magento根文件夹中

我如何下载该文件

$filename
上载了来自数据库的文件名

编辑:

当我删除代码时:

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);
    }
 if (! is_file ( $filepath ) || ! is_readable ( $filepath )) {
            throw new Exception ( );
        }
然后将文件路径更改为:

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

然后完全完成下载…

这是此类问题的解决方案:

 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;
    }

基于文件路径的问题…现在已经解决了…

如何使用Magento代码<代码>\u preparedownloadsresponse()


当您有权访问文件路径时,要在控制器操作中使用Magento的
\u PreparedDownloadResponse()
,请在控制器类中执行以下操作:

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

    return $this->_prepareDownloadResponse(basename($filePath), file_get_contents($filePath));
}

它更倾向于使用@WonderLand建议的
\u preparedownloadsresponse()
,因为它不需要
退出
我正在尝试实现相同的解决方案,但它给我的错误是“在非对象上调用成员函数setHttpResponseCode()”。您能告诉我文件中还需要包含其他类吗?谢谢。也尝试使用了_preparedownloadsresponse,但不起作用。这肯定是可以接受的答案。谢谢@Wonderland是否可以使用\u PreparedDownloadResponse下载多个文件?是否可以使用\u PreparedDownloadResponse下载多个文件?