Reputation: 145
if I clicked a link with url : website.com/open/pdf/Document_One_Drag_3.pdf, it will open the pdf in the browser instead of download. This is my code in Route.php
Route::get('/open/pdf/{filename}', function($filename)
{
// Check if file exists in app/storage/file folder
$file_path = public_path('uploads/docs/'.$filename);
if (file_exists($file_path))
{
return Response::make(file_get_contents($file_path), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; '.$filename,
]);
}
else
{
// Error
exit('Requested file does not exist on our server!');
}
});
the PDF file still downloading and not opened in the browser. What is wrong ?
Upvotes: 1
Views: 3578
Reputation: 481
Considering Laravel 5.2, The download method may be used to generate a response that forces the user's browser to download the file at the given path.
$pathToFile = public_path(). "/download/fileName.pdf";
return response()->download($pathToFile);
The download method accepts a file name as the second argument to the method, which will determine the file name that is seen by the user downloading the file. Finally, you may pass an array of HTTP headers as the third argument to the method:
return response()->download($pathToFile, $name, $headers);
So, you may use
$headers = ['Content-Type' => 'application/pdf'];
as the third parameter.
This information might be useful too:
Note: Symfony HttpFoundation, which manages file downloads, requires the file being downloaded to have an ASCII file name.
To open in browser
<a href="{{url('/your/path/to/pdf/')}}" target="_blank" >My PDf </a>
Upvotes: 1