1 year ago

Laravel file view response - download prompted with file name as last part of route

Posted 1 year ago by DavidPetrov

It's a quite simple problem but I can't find anything on the topic online, only a couple of posts with the same problem but no solution. Baiscally, I've got a controller method which returns a file to the user to view:

    public function viewFile(Request $req)
        return response()->file(Storage::disk('files')->url(urldecode($req->rel_path)));

The url is the following: https://myserver.com/viewFile?rel_path=order%2F5366%2FIMG_20190415_093933.jpg and the file is getting displayed properly. But when a user prompts to download it, browsers suggest the last part of the route (in this case - viewFile) as the file's name instead of its real name. So the data is kinda lost (or a user has to type the name in manually from the link), which is not such a big matter but it's overcomplicating things.

I tried setting filename in the header as suggested here: https://stackoverflow.com/a/43491469/5885062, but without success.

Any ideas? Thanks in advance!

Please sign in or create an account to participate in this conversation.