我有以下代码可以在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
用来测试时,会出现
我该如何解决?
尝试为PDF使用特定的MIME类型:
header("Content-Type: application/pdf");
附带一提,您可以使用Laravel的downlod方法,该方法应该处理所有事情(它足够聪明,可以正确猜测MIME类型并提供正确的标题),而无需手动创建所有标题:
return Response::download($filePath, $fileClientName);
您也可以手动将标头指定为第3个参数,但是每次我使用此参数下载PDF时,它都立即起作用,因此没有必要。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句