【问题标题】:Laravel Storrage get file causes problem in Google ChromeLaravel Storage 获取文件导致 Google Chrome 出现问题
【发布时间】:2021-01-11 20:51:17
【问题描述】:

我试图从我的 Laravel 应用程序的存储中获取文件,如下所示:(注意:在 Firefox 中直接显示 pdf / jpeg 格式的文件。其他文件(如 docx 或其他文件导致下载)。

    $contents = Storage::get("files/" . $file);
    return $contents;

在谷歌浏览器中文件没有显示,只显示这样的文件源(如果我有 jpeg 图像,以下是示例)我的整个桌面充满了以下迹象:

ÿØÿà�JFIF������ÿÛ�„�

我是否必须设置标题或其他内容才能在 Google Chrome 中显示文件?

【问题讨论】:

  • 那是哪种文件格式?
  • 这是 jpeg 图片的第一行
  • 请分享更多细节。您在哪里设置该响应的标头?
  • @SaschaK 你想要什么?下载或获取网址?
  • 我现在不设置标题。我只是获取文件并返回以在浏览器中显示文件,或者如果浏览器无法显示它应该下载。

标签: laravel storage


【解决方案1】:

你可以试试:

$content = Storage::get("files/" . $file);
if (!File::exists($content)) {
    abort(404);
}
$file = Storage::get($content);
$type = Storage::mimeType($content);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);

return $response;

【讨论】:

  • 此解决方案向我发送“500 内部服务器错误”,错误:ArrayBuffer
【解决方案2】:

试试这个。

$file = File::get("files/" . $file);
$type = File::mimeType("files/" . $file); 
$response = Response::make($file, 200);
$response->header("Content-Type", $type);

【讨论】:

  • $response = Response::make($file, 200);这带来了错误 500。为什么?
  • 您应该使用 try catch 块来查找确切的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 2013-01-02
  • 2018-07-30
相关资源
最近更新 更多