2016-03-07 44 views
0

我有一个页面,用户点击“下载”按钮。 这将打开一个模式,该模式需要CODE授权下载。Bootstrap modal + readFile:如何强制文件下载?

一旦插入,代码是ajax-提交给PHP脚本验证代码并恢复文件OUTSIDE webroot并需要强制下载。

但是,我看到萤火虫那个脚本“相呼应”的文件内容...

我需要“关闭”模式,并强制下载。非常感谢你!

HTML(模式)

<div class="modal fade" id="downloadpayroll" tabindex="-1" role="dialog" aria-labelledby="downloadpayrolllabel"> 
    <div class="modal-dialog" role="document"> 
     <div class="modal-content" id="modal-form-container"> 
      <form method="post" class="form" id="enable-payroll-download" action="{{ @ABSOLUTE_PATH }}{{ @ALIASES.url_payroll_new }}"> 
       <input type="hidden" name="payroll_id" value="" /> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h4 class="modal-title" id="myModalLabel">Conferma download</h4> 
       </div> 
       <div class="modal-body"> 
        <p>Per confermare il download, <strong>inserisci il tuo PIN</strong></p> 

         <div class="form-group"> 
          <label for="pin">PIN</label> 
          <input type="text" class="form-control input-lg text-center required-field numbers-only" name="pin" /> 
         </div> 

       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button> 
        <button type="submit" class="btn btn-primary">Scarica cedolino</button> 
       </div> 
      </form> 
     </div> 


    </div> 
</div> 

JQUERY提交

var data = $form.serialize(); 
      data = data+'&ajax=true'; 
      $.ajax({ 
        data : data, 
        type : $form.attr('method'), 
        url  : $form.attr('action'), 
        success : function(response) { 
         // nothing to do here 
        } // success 
      }); 

PHP脚本

// other stuffssss 
if ($validate===true) { 
       // IL PIN E' CORRETTO, TIRIAMO FUORI IL CEDOLINO 
       $payroll = $this->service->getPayroll($id_payroll,'id',null,'ASC',1); 
       $path = \SupportText::removeSubstringFromString($this->f3->get('SERVER.DOCUMENT_ROOT'), '/httpdocs').'/private'; 
       $filename = $path.'/'.$this->id_user.'/'.$payroll[0]->filename; 
       \SupportFile::returnFile($filename); 
       exit; 
      } 

PHP SupportFile类

class SupportFile { 

    public static function returnFile($filename) { 
     // Check if file exists, if it is not here return false: 
     if (!file_exists($filename)) return false; 
     header('Content-Description: File Transfer'); 
     header('Content-Type: application/octet-stream'); 
     // Suggest better filename for browser to use when saving file: 
     header('Content-Disposition: attachment; filename='.basename($filename)); 
     header('Content-Transfer-Encoding: binary'); 
     // Caching headers: 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     // This should be set: 
     header('Content-Length: ' . filesize($filename)); 
     // Clean output buffer without sending it, alternatively you can do ob_end_clean(); to also turn off buffering. 
     ob_clean(); 
     // And flush buffers, don't know actually why but php manual seems recommending it: 
     flush(); 
     // Read file and output it's contents: 
     readfile($filename); 
     // You need to exit after that or at least make sure that anything other is not echoed out: 
     exit; 
    } 

} 
+1

更好地为您的文件的一些临时航线,然后当阿贾克斯完成了对文件donload – madalinivascu

+0

@madalinivascu试图与临时航线重定向到。作品。谢谢 ;) – sineverba

回答

0

尝试使用ob_start();在header()之前。然后使用代码如下

class SupportFile { 

public static function returnFile($filename) { 
    if (!file_exists($filename)) return false; 

    ob_strat(); 

    header('Content-Description: File Transfer'); 
    header('Content-Disposition: attachment; filename='.basename($filename)); 
    header("Content-Length: " . filesize($filename)); 
    header('Content-Type: '.mime_content_type($filename)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($filename)); 
    ob_end_clean(); 
    ob_clean(); 
    flush(); 
    readfile($filename); 
    exit; 
    } 
}