简体   繁体   中英

Download files from Laravel

I would like create endpoint in Laravel which I can use in Linux to download zip archive.

I created:

public function download_templates()
{
    return response()->file(base_path() . '/sites/templates.zip');
}

But when I use:

wget http://linktopage.com/api/download_templates

In result I receive HTML code of endpoint.

Someone has idea?

To initialize file download response should contain headers indicating that it's attachment otherwise browser will print content.

According to the laravel docs "file" method is used in case you want just to output file contents without downloading. For downloading there is method "download" which forces download.

just use this code if you are using 5.*

return response()->download(public_path('/sites/templates.zip'));

also, I changed base_path() to public_path() , so you have to put the file in Public, if not you can use the base_path() instead of public_path() in the code above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM