[英]Return file download from storage in Laravel
我希望能够从我的 controller 中的存储中检索文件。
public function getFile($fileID) {
$file = CommUploads::where('id', $fileID)->first();
if (Auth::user()->id == $file->user_id || Auth::user()->id == $file->artist_id) {
$fileGet = Storage::get($file->file_path);
return $fileGet;
}
}
现在,我能够正确获取路径,但是,出现的所有内容都是胡言乱语,就好像您正在查看没有扩展名的图像一样。
理想情况下,我希望这可以作为“另存为”之类的东西。
我怎样才能做到这一点?
您应该使用文件下载响应 。
public function getFile($fileID) {
$file = CommUploads::where('id', $fileID)->first();
if (Auth::user()->id == $file->user_id || Auth::user()->id == $file->artist_id) {
return response()->download(storage_path('app/' . $file->file_path));
}
}
返回一个字符串是不好的做法。 使用Laravel响应时 ,Laravel确保正确设置标头和其他要求以将响应返回给浏览器。
您可以使用方法download
而不是get
Storage
门面。
public function getFile($fileID) {
$file = CommUploads::where('id', $fileID)->first();
if (Auth::user()->id == $file->user_id || Auth::user()->id == $file->artist_id) {
return Storage::download($file->file_path);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.