Just add some header content-types? By default browsers will show PDFs and download docx and doc etc
Oct 4, 2014
16
Level 51
Opening files online using Laravel
Hi all,
I have built a small app that allows file uploads, but I want to be able to click a link that either opens in the browser (for example if the file is PDF) otherwise if it's a Word Document it should be downloaded.
Is there any package to do this, or is there a way to do this?
Level 65
Could use a more Laravel way
if (File::isFile($file))
{
$file = File::get($file);
$response = Response::make($file, 200);
// using this will allow you to do some checks on it (if pdf/docx/doc/xls/xlsx)
$response->header('Content-Type', 'application/pdf');
return $response;
}
// Or to download
if (File::isFile($file))
{
return Response::download($file, $file_name);
}
Some content types
$content_types = [
'application/octet-stream', // txt etc
'application/msword', // doc
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', //docx
'application/vnd.ms-excel', // xls
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // xlsx
'application/pdf', // pdf
];
6 likes
Please or to participate in this conversation.