我有以下代码以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' => 'ServicioController@doDownload'));
当我使用Postman
进行测试时,会出现
我该如何解决?
答案 0 :(得分:1)
尝试使用pdf的specfic MIME类型:
header("Content-Type: application/pdf");
在旁注中,您可以使用Laravel's downlod method来处理所有内容(它足够聪明,可以正确猜出MIME类型并提供正确的标题),而无需您创建所有内容手动标题:
return Response::download($filePath, $fileClientName);
您也可以手动将标题指定为第3个参数,但每次我使用它来下载它可以直接使用的PDF,所以它不是必需的。