2015-07-20 108 views
0

我使用Laravel的存储门面下载PDF文件,我能够上传PDF到S3,我也能得到()的内容,但我不能显示或将其下载到最终用户作为实际的pdf文件。它看起来像原始数据。这里是代码:Laravel 5.1 - 如何从S3斗

$file = Storage::disk($storageLocation)->get($urlToPDF); 
header("Content-type: application/pdf"); 
header("Content-Disposition: attachment; filename='file.pdf'"); 
echo $file; 

这怎么办?我检查了几篇文章(和SO),但他们都没有为我工作。

回答

-2

我想通了。愚蠢的错误。我不得不从文件名中删除单引号。

修复:

$file = Storage::disk($storageLocation)->get($urlToPDF); 
header("Content-type: application/pdf"); 
header("Content-Disposition: attachment; filename=file.pdf"); 
echo $file; 
+0

为什么这个downvoted即使它被接受? –

+0

这是一个不好的解决方案 –

2

你可以创建一个下载网址,使用getObjectUrl方法

财产以后这样的:

$downloadUrl = $s3->getObjectUrl($bucketname, $file, '+5 minutes', array(
      'ResponseContentDisposition' => 'attachment; filename=$file,'Content-Type' => 'application/octet-stream', 
    )); 

和URL传递给用户。这将引导用户进入一个amzon页面,该页面将开始文件下载(该链接将有效5分钟 - 但你可以改变它)

另一种选择,首先将该文件保存到您的服务器,然后让用户从您的服务器下载文件

4

我觉得这样的事情会做的工作在15.2:

public function download($path) 
{ 
    $fs = Storage::getDriver(); 
    $stream = $fs->readStream($path); 
    return \Response::stream(function() use($stream) { 
     fpassthru($stream); 
    }, 200, [ 
     "Content-Type" => $fs->getMimetype($path), 
     "Content-Length" => $fs->getSize($path), 
     "Content-disposition" => "attachment; filename=\"" .basename($path) . "\"", 
     ]); 
} 
0
$filename = 'test.pdf'; 
$filePath = storage_path($filename); 

$header = [ 
    'Content-Type' => 'application/pdf', 
    'Content-Disposition' => 'inline; filename="'.$filename.'"' 
]; 

return Response::make(file_get_contents($filePath), 200, $header); 
+3

感谢您的第一篇文章。在答案中发布代码时,尽量避免仅发布代码块,通过提供有关更改内容和原因的解释来扩展答案。请参阅[社区指南](https://stackoverflow.com/help/how-to-answer)撰写一个好的答案。 – LightBender

+2

有一点解释可以帮助你更好地理解你的答案。 – Annjawn