Sunday, September 27, 2020

Laravel file download code

private $mimeTypeArray = [
'pdf' => "application/pdf",
'doc' => "application/msword",
'docx' => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
];

public function download($type, $file)
{
if ($type == 'resume') {
$path = 'storage' . DIRECTORY_SEPARATOR . 'resumes';
}
//PDF file is stored under project/public/download/info.pdf
$file = public_path($path . DIRECTORY_SEPARATOR . $file);

$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));

$headers = [
'Content-Type: ' . Arr::get($this->mimeTypeArray, $ext),
];

return response()->download($file, $type . '-' . Str::random(6) . '.' . $ext, $headers);

} 

No comments:

Post a Comment