5

我在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);
    }

但它确实显示错误,例如:

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}

uploads文件夹位于magento根文件夹中...

怎么下载文件啊。。。。

$filename上传来自数据库的文件名...

编辑 :

当我删除代码时:

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

然后将文件路径更改为:

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

然后完美下载完成......

4

3 回答 3

12

使用 Magento 代码怎么样?..._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;
        }
    }
于 2015-10-09T11:30:12.927 回答
10

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

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

基于文件路径问题的问题......现在解决了......

于 2013-03-12T05:33:22.777 回答
0

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

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

    return $this->_prepareDownloadResponse(basename($filePath), file_get_contents($filePath));
}
于 2018-10-22T13:01:05.403 回答