Laravel在公共文件夹中下载pdf [英] Laravel download pdf in public folder

查看:89
本文介绍了Laravel在公共文件夹中下载pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在公开/下载中有一个pdf文件.我只想链接到它,然后下载或在浏览器中打开它.我尝试在浏览器中点击http://localhost:8000/downloads/brochure.pdf,但是我只得到了白色屏幕,没有任何错误.在Chrome DevTools>网络中,该请求已被取消.我正在通过javascript插入URL,因此无法像这里建议的其他答案一样使用URL :: to或link_to().

I have a a pdf in public/downloads. I want to just link to it and have it download or open in browser. I tried hitting http://localhost:8000/downloads/brochure.pdf in the browser but I just get a white screen with no errors. In Chrome DevTools > Network, it says the request was canceled. I'm inserting the URL via javascript so I can't use URL::to or link_to() like other answers on here have suggested.

注意:当我以与Brochure.pdf相同的方式链接到css文件时,css出现在浏览器中.

Note: When I link to a css file in the same fashion as the brochure.pdf, the css appears in the browser.

推荐答案

使用以下代码可以实现文件路径:

Path to file can be achieved with the following code:

public function getDownload(){

        $file = public_path()."/downloads/info.pdf";
        $headers = array('Content-Type: application/pdf',);
        return Response::download($file, 'info.pdf',$headers);
    }

该功能将从'project/public/downloads'文件夹中下载文件.

function will download file from : 'project/public/downloads' folder.

(不要忘记自己设置路线和控制器)

(don't forget to set-up routes and controller by yourself)

这篇关于Laravel在公共文件夹中下载pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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