Laravel - 文件下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36437969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:32:52  来源:igfitidea点击:

Laravel - File Downloads

phplaravellaravel-5

提问by NSaid

Hi I'm trying to fix a Laravel download bug I am dealing with. I have the correct route setup, and correct function in the controller. I also am able to confirm that I have access to the file because I created a file using the exact same route and returned it. By doing this I was able to successfully return the contents of the file. However when I try and use a button from the view and call the controller function I get this error:

嗨,我正在尝试修复我正在处理的 Laravel 下载错误。我的路线设置正确,控制器功能正确。我还能够确认我有权访问该文件,因为我使用完全相同的路径创建了一个文件并将其返回。通过这样做,我能够成功返回文件的内容。但是,当我尝试使用视图中的按钮并调用控制器函数时,我收到此错误:

FileNotFoundException in File.php line 37:
The file "The file "2016-04-04_07-21-50 - Pinging host: 192.168.2.1
2016-04-04_07-21-50 - Host 192.168.2.1 is up!
2016-04-04_07-21-50 - Pinging host: 192.168.2.2
2016-04-04_07-21-53 - Pinging host: 192.168.2.3 ...

Now here is the code that resulted in this error:

现在这是导致此错误的代码:

show.blade.php

show.blade.php

<a class="btn btn-default col-md-12" href="/getDownload/{{ $now }}" role="button">Download Today's Log</a>

HonoursController.php

荣誉控制器.php

public function getDownload($id)
    {
      $file = File::get("../resources/logs/$id");
      $headers = array(
           'Content-Type: application/octet-stream',
      );
      #return Response::download($file, $id. '.' .$type, $headers); 
      return response()->download($file, $id.'txt', $headers);
    }

What I have been able to surmise is that I am getting a 500 HTTP error. My inspection does not provide me with any other information however. Any idea what is going on?

我能够推测的是,我收到了 500 HTTP 错误。然而,我的检查没有为我提供任何其他信息。知道发生了什么吗?

采纳答案by nick.graziano

Try this:

尝试这个:

public function getDownload($id)
{
    // $file = File::get("../resources/logs/$id");
    $headers = array(
       'Content-Type: application/octet-stream',
    );
    #return Response::download($file, $id. '.' .$type, $headers); 
    return response()->download("../resources/logs/$id", $id.'txt', $headers);
}

From the docs:

从文档:

The download method may be used to generate a response that forces the user's browser to download the file at the given path.

return response()->download($pathToFile, $name, $headers);

download 方法可用于生成响应,强制用户的浏览器在给定路径下载文件。

返回响应()->下载($pathToFile, $name, $headers);

https://laravel.com/docs/5.1/responses#basic-responses

https://laravel.com/docs/5.1/responses#basic-responses

回答by jakub_jo

The first argument of the download method should be a path to the file, not the file itself.

下载方法的第一个参数应该是文件的路径,而不是文件本身。

The download method may be used to generate a response that forces the user's browser to download the file at the given path. ...

Source: https://laravel.com/docs/5.2/responses#file-downloads

download 方法可用于生成响应,强制用户的浏览器在给定路径下载文件。...

来源:https: //laravel.com/docs/5.2/responses#file-downloads

回答by Ramesh KC

Follow these two steps:

请按照以下两个步骤操作:

  1. Find the details of the file by using the Model.
  2. Use Laravel Response to download file with the help of headers.
  1. 使用模型查找文件的详细信息。
  2. 使用 Laravel Response 在标头的帮助下下载文件。

Here Blog is the model and $id is the primary key which indicates file. Our table's column name where file name has been stored is cover_image, so $file_name=$blog->cover_image;provides us the file name. Let's assume our file is present in upload/images/ of public folder of Laravel.

这里Blog是model,$id是主键,表示文件。我们表中存储文件名的列名是cover_image,所以$file_name=$blog->cover_image; 为我们提供文件名。假设我们的文件存在于 Laravel 公共文件夹的 upload/images/ 中。

Controller

控制器

`public function download(Blog $blog,$id){
    $blog=$blog->find($id);
    $headers = array(
        'Content-Type: application/octet-stream',
     );
    $pathToFile=public_path('upload/images/');
    $file_name=$blog->cover_image;
    $download_name='Download-'.$file_name;
    return response()->download($pathToFile.$file_name, $download_name, $headers);
 }`

Route

路线

`Route::get('{id}/file-download',['as'=>'file-download','uses'=>'BlogsController@download']); ` 

View

看法

Here, ['id'=>1] indicates we are going to download file having primary key as 1. If you want to download another just change with any n integer number.

这里,['id'=>1] 表示我们要下载主键为 1 的文件。如果你想下载另一个,只需更改任意 n 个整数即可。

`<a class="btn btn-primary" href="{{ route('file-download',['id'=>1]) }}">Download</a>

`

`