2016-06-10 37 views
1

我成功使用Laravel 5.2將.pdf文件上傳到我的AWS S3存儲桶。我可以直接前往AWS S3上的存儲桶並下載每個.pdf,而不會出現問題。當我嘗試使用下面的代碼下載某些.pdf時,該文件無法被瀏覽器或Acrobat Pro打開。下面是代碼:某些.pdf文件下載在瀏覽器中損壞/ Acrobat Pro - 使用Laravel和AWS S3

$file = Storage::disk('s3')->get($myfile); 

// Set headers and force download 
header("Content-type:application/pdf"); 
header("Content-Disposition:attachment;filename=file.pdf"); 
echo $file; 

在瀏覽器中,我得到了以下錯誤:Failed to load PDF document 在的Acrobat Pro,我得到以下錯誤:There was an error opening this document. The file is damaged and could not be repaired.

的.PDF是能夠在其他蘋果成功打開程序,例如Preview或Skim。另外,可以成功上傳和下載較舊的.pdf文件。

回答

0

雖然我不清楚以前使用echo $file;是什麼問題,但現在可以在修改爲使用Laravel方法return Response::make($file, 200, $headers);時使用。

// Get the file from s3 
$file = Storage::disk('s3')->get($myfile); 

// Set headers 
$headers = array(
    'Content-type' => 'application/pdf', 
    'Content-Disposition' => 'attachment; filename=filename=file.pdf" 
); 

// Download 
return Response::make($file, 200, $headers);