2017-05-01 3 views
0

Hier habe ich eine Schaltfläche, wenn ich auf die Schaltfläche klicken, bedeutet, ich möchte die eine demo.xls-Datei herunterladen, mit php ich tat, aber jetzt möchte ich Codeigniter tun, versuchte ich aber Ich kann das nicht? finden Sie in meinem Code untenSo laden Sie die xls-Datei mit codenigniter

<button class="btn btn-warning" id="download-btn"> 
    <i class="fa fa-download" aria-hidden="true"></i> Download Demo File 
</button> 


<script type="text/javascript"> 
$(document).ready(function(){ 
$("#download-btn").click(function(e){ 
    e.preventDefault(); 
    $.ajax({ 
      type:'POST', 
      url :"Staff/downloadDemoFile", 
      cache: false, 
      contentType: false, 
      processData: false, 
      success: function(data) { 
       console.log(data); 
      }, 
      error:function(exception){ 
      alert('Exeption:'+exception); 
      } 
      }); 


}); 
}); 
</script> 

Mein Controller

public function downloadDemoFile() 
{ 
    if($this->session->logged_in != TRUE){ 
     $this->load->view('login'); 
    } 
    else{ 
    $download= $this->Add_staff_model->downloadFile(); 
    } 
} 

Mein Modell

public function downloadFile() 
    { 
      $sFileName = 'demo.xls'; 
      header("Cache-Control: public"); 
      header("Content-Description: File Transfer"); 
      header("Content-Disposition: attachment; filename=$sFileName"); 
      header("Content-Type: application/zip"); 
      header("Content-Transfer-Encoding: binary"); 

      // read the file from disk 
      readfile(UPLOAD_PATH_XLS.$sFileName); 

    } 
+0

Wenn Ihr Code ohne Codeigniter (reines PHP) funktioniert, verwenden Sie den gleichen Code innerhalb der Codeigniter-Anwendung. Beide sind php, es sollte funktionieren. – shaggy

Antwort

0

eine Datei von der Festplatte herunterzuladen ist ziemlich straightfoward mit CodeIgniter Download Helfer. Lesen Sie das Handbuch here.

public function downloadFile() 
{ 
    // load the helper somewhere, i.e in the constructor. 
    $this->load->helper('download'); 

    // Use it when necessary 
    $sFileName = 'demo.xls'; // filename to be download. With path if necessary: /path/to/demo-xls 
    force_download($sFileName, NULL); 
} 

Als letzte Überlegung, in einer solchen Situation würde ich legte das downloadFile() Methode im Modell aber in der Steuerung selbst nicht.