2013-03-12 12 views
5

ho caricato alcuni file per ogni cliente in Magento ....Come scaricare i file in Magento

Poi ho elencato i dettagli dei clienti con il nome del file caricato ..

Ho bisogno di scaricare il file utilizzando il codice di Magento

Questo è il codice:

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

Ma didsplays errori qualcosa come:

01.235.
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} 

La cartella uploads è nella cartella principale Magento ...

Come faccio a scaricare il file ....

Il $filename hanno il nome del file caricato che proviene da database ...

EDIT:

Quando ho rimosso il codice:

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

poi cambiato il percorso del file come:

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

quindi scaricando fatto perfettamente ....

+0

ok capito ora .... – Kichu

risposta

10

Questa è la soluzione per questo tipo di problemi:

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

Il problema basa sui problemi del percorso del file ..... ora è risolto ....

+0

Si è preferito utilizzare '_prepareDownloadResponse()', come suggerito da @WonderLand in quanto non ha bisogno di 'exit' – naitsirch

+0

Sto cercando di implementare la stessa soluzione ma mi sta dando errore come "Chiamata a una funzione membro setHttpResponseCode() su un non oggetto", per favore fammi sapere c'è qualche altra classe che deve includere nel file? Grazie. –

+0

Anche provato a usare _prepareDownloadResponse ma non funziona. –

10

E tu? se codice 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; 
     } 
    } 
+5

Questa dovrebbe essere la risposta accettata. Grazie a @WonderLand – naitsirch

Problemi correlati