Answer the question
In order to leave comments, you need to log in
How to change the header content-type when returning a file using the Storage::get($file) method?
I do the return of documents using php, I specify the Content-Type. And when you click on a link, the type is always defined as text / html
, and if you open the file in the browser, accordingly, it turns out to be crooked. If you save, then everything is OK. Why does explicit content-type not work?
public function getDocument($file)
{
// получаем всю инфу о файле:
$saveFile=FileMeeting::find($file);
// получаем путь к файлу:
$dir='meeting/'.$saveFile->meeting_id.'/'.$saveFile->filename;
// получаем его размер
$size = Storage::size($dir);
// получаем расширение
$ext = substr(strrchr($saveFile->filename, '.'), 1);
$mime = MimeType::detectByFileExtension($ext);
// приводим имя файла к божескому виду из титла
$nameToGet = preg_replace('/[^\p{L}0-9]/u', '', $saveFile->title);
// сбрасываем буфер вывода PHP, чтобы избежать переполнения памяти выделенной под скрипт
// если этого не сделать файл будет читаться в память полностью!
if (ob_get_level()) {
ob_end_clean();
}
// заставляем браузер показать окно сохранения файла
header('Content-Description: File Transfer');
//header("Content-type: application/pdf");
header('Content-type: '.$mime);
header('Content-Disposition: attachment; filename=doc.pdf');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . $size);
// читаем файл и отправляем его пользователю
return Storage::get($dir);
exit;
}
return response()->download($dir,$nameToGet);
Answer the question
In order to leave comments, you need to log in
Because Laravel wraps the response in its Response, which specifies text/html.
You don't need to manually send a header, you need to return a Response object in which to specify the headers, then everything will work.
You could open the documentation yourself and find laravel.com/docs/5.1/responses#file-downloads
Why, give the file through puff?
a) until the file is given to the user, the script will work for you.
It is better to assign this to the server via X-Accel-Redirect
valera.ws/2012.03.06~accel-redirect-apache-dokachka
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question