Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
854 views
in Technique[技术] by (71.8m points)

storage - Laravel Storrage get file causes problem in Google Chrome

I tried to get file from Storage of my Laravel Application like following: (Notice: In Firefox Files in pdf / jpeg format are directly been showed. Other files like docx or something is causing download).

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

In Google Chrome file is not showing and just shows me source of file like this (Following is example if I have a jpeg image) My whole desktop is full of follwing signs:

???à?JFIF???????????

Do I have to set headers or something to show files in Google Chrome?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try instead :

$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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...