简体   繁体   English

从 Laravel 下载文件

[英]Download files from Laravel

I would like create endpoint in Laravel which I can use in Linux to download zip archive.我想在 Laravel 中创建端点,我可以在 Linux 中使用它来下载 zip 存档。

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.结果我收到端点的 HTML 代码。

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.根据laravel docs ,如果您只想输出文件内容而不下载,则使用“文件”方法。 For downloading there is method "download" which forces download.对于下载,有强制下载的方法“下载”。

just use this code if you are using 5.*如果您使用的是 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.另外,我将base_path()更改为public_path() ,因此您必须将文件放在 Public 中,否则您可以在上面的代码中使用base_path()而不是public_path()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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