2014-06-09 41 views
1

我有如下的代碼來下載一個PDF在laravel爲什麼當我嘗試在laravel下載PDF文件時,打開一個具有奇怪字符的文件?

控制器

public function doDownload($id){ 

     $servicio = Servicio::find($id); 
     $array = explode('/', $servicio->RutaPDF); 
     $path = '/home/web-apps/marsur/public/servicio/'.$array[1].'/ordedeservicio.pdf'; //array[1] contain a number 

     header("Content-Type: application/octet-stream"); 

     header("Content-Disposition: attachment; filename=" . urlencode($path)); 
     header("Content-Type: application/octet-stream"); 
     header("Content-Type: application/download"); 
     header("Content-Description: File Transfer");    
     header("Content-Length: " . filesize($path)); 
     flush(); // this doesn't really matter. 
     $fp = fopen($path, "r"); 
     while (!feof($fp)) 
     { 
      echo fread($fp, 65536); 
      flush(); // this is essential for large downloads 
     } 
     fclose($fp); 
} 

路線

Route::get('servicio/download/{id}', array('uses' => '[email protected]')); 

,當我用Postman測試,這似乎

enter image description here

我該如何解決?

回答

1

嘗試使用對於PDF的specfic MIME類型:

header("Content-Type: application/pdf"); 

在一個側面說明,您可以使用Laravel's downlod method應該照顧一切(這是夠聰明,猜中MIME類型,並提供正確的頭),無需你手動創建所有的頭:

return Response::download($filePath, $fileClientName); 

您也可以手動指定頭作爲第三個參數,但每次我用它來下載一個PDF它的工作馬上,因此它不應該是必要的。

相關問題