4

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.

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

Ben
  • 291
  • 1
  • 3
  • 16

2 Answers2

11

Path to file can be achieved like:

public function getDownload(){

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

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

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

DPP
  • 12,716
  • 3
  • 49
  • 46
5

Assuming you have stored the file in the public folder under a folder called file and the file name is called download.pdf, (public/file/download.pdf);

This is a minimal working alternative without Laravel facade.

Optional arguments can be added to ->download() method: https://laravel.com/docs/9.x/responses#file-downloads

Working with php 7.4+

use Symfony\Component\HttpFoundation\BinaryFileResponse;

public function downloadPdfFile() : BinaryFileResponse
{
     return response()->download(public_path('file/download.pdf'));
}
Alessandro
  • 409
  • 5
  • 12
Mundruku
  • 229
  • 4
  • 5