Laravel-在不强制下载的情况下显示存储中的PDF文件吗? [英] Laravel - display a PDF file in storage without forcing download?

查看:131
本文介绍了Laravel-在不强制下载的情况下显示存储中的PDF文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储在app/storage/中的PDF文件,并且我希望经过身份验证的用户能够查看此文件.我知道我可以让他们使用

I have a PDF file stored in app/storage/, and I want authenticated users to be able to view this file. I know that I can make them download it using

return Response::download($path, $filename, $headers);

但是我想知道是否有一种方法可以使他们直接在浏览器中查看文件,例如,当他们将Google Chrome与内置的PDF查看器一起使用时.任何帮助将不胜感激!

but I was wondering if there is a way to make them view the file directly in the browser, for example when they are using Google Chrome with the built-in PDF viewer. Any help will be appreciated!

推荐答案

2017年更新

其他响应类型中记录的Laravel 5.2开始,您现在可以使用文件助手在用户浏览器中显示文件.

As of Laravel 5.2 documented under Other response types you can now use the file helper to display a file in the user's browser.

return response()->file($pathToFile);

return response()->file($pathToFile, $headers);

来源/感谢以下回答

2014年过时的答案

您只需要将文件的内容发送到浏览器并告诉其内容类型,而不是告诉浏览器下载它即可.

You just need to send the contents of the file to the browser and tell it the content type rather than tell the browser to download it.

$filename = 'test.pdf';
$path = storage_path($filename);

return Response::make(file_get_contents($path), 200, [
    'Content-Type' => 'application/pdf',
    'Content-Disposition' => 'inline; filename="'.$filename.'"'
]);

如果使用Response::download,它将自动将Content-Disposition设置为附件,这将导致浏览器下载它.有关内容处置之间的区别,请参见此问题内联和附件.

If you use Response::download it automatically sets the Content-Disposition to attachment which causes the browser to download it. See this question for the differences between Content-Disposition inline and attachment.

根据注释中的要求,我应该指出,您需要在文件开头处use Response才能使用Facade.

As per the request in the comments, I should point out that you'd need to use Response at the beginning of your file in order to use the Facade.

use Response;

或完全限定的名称空间(如果Response没有别名为Illuminate的响应外观).

Or the fully qualified namespace if Response isn't aliased to Illuminate's Response Facade.

这篇关于Laravel-在不强制下载的情况下显示存储中的PDF文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆