我使用的是laravel 5.2,我使用过response()-> file()函数返回文件。在localhost上,它可以按预期工作,但在实时服务器文件上会自动下载文件(无扩展名)。但是我希望打开它,而不是向下看。有人可以帮忙吗?
这是我的代码:
公共函数returnFile($ slug)
{$file = Mixes::where('id_name,'=',$slug)->get()->first();
return response()->file('./path/to/file/'.$file->name);}
Run Code Online (Sandbox Code Playgroud)
谢谢。
您需要在响应中添加标题。
标题示例的响应:
$response->header('Content-Type', 'application/pdf');
Run Code Online (Sandbox Code Playgroud)
简单的例子:
$file = File::get($file);
$response = Response::make($file, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
Run Code Online (Sandbox Code Playgroud)
然后,该文件将显示在浏览器窗口中。
该解决方案将与pdf,docx,doc,xls文件一起使用。
希望对您有所帮助!