Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k 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 Reply

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...