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 に答える