【问题标题】:Download files in laravel using Response::download使用 Response::download 在 laravel 中下载文件
【发布时间】:2013-12-23 07:19:32
【问题描述】:

在 Laravel 应用程序中,我试图在视图中实现一个按钮,该按钮可以允许用户下载文件而无需导航到任何其他视图或路线 现在我有两个问题: (1) 下面的函数抛出

The file "/public/download/info.pdf" does not exist

(2) 下载按钮不应将用户导航到任何地方,而应仅在同一视图上下载文件,我当前的设置,将视图路由到“/下载”

这是我尝试实现的方法:

按钮:

  <a href="/download" class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a>

路线:

Route::get('/download', 'HomeController@getDownload');

控制器:

public function getDownload(){
        //PDF file is stored under project/public/download/info.pdf
        $file="./download/info.pdf";
        return Response::download($file);
}

【问题讨论】:

    标签: php laravel laravel-4 laravel-routing


    【解决方案1】:

    试试这个。

    public function getDownload()
    {
        //PDF file is stored under project/public/download/info.pdf
        $file= public_path(). "/download/info.pdf";
    
        $headers = array(
                  'Content-Type: application/pdf',
                );
    
        return Response::download($file, 'filename.pdf', $headers);
    }
    

    "./download/info.pdf" 将不起作用,因为您必须提供完整的物理路径。

    2016 年 5 月 20 日更新

    Laravel 5、5.1、5.2 或 5.* 用户可以使用以下方法代替 Response 门面。但是,我之前的答案适用于 Laravel 4 或 5。($header 数组结构更改为关联数组=&gt;- 'Content-Type' 之后的冒号被删除 - 如果我们不做这些更改,那么标题将以错误的方式添加:标题的名称将是从 0,1 开始的数字,...)

    $headers = [
                  'Content-Type' => 'application/pdf',
               ];
    
    return response()->download($file, 'filename.pdf', $headers);
    

    【讨论】:

    • 有什么方法可以返回文件下载并更新视图?
    • 我可以只在下载时更改文件权限吗? @Anam
    • @EswaraReddy,你的意思是在飞行中?我不这么认为。
    • 如何下载任何类型的文件?
    • @SazzadTusharKhan 只需使用return Response::download($pathToFile);
    【解决方案2】:

    Laravel 5 中的文件下载非常简单。

    正如@Ashwani 提到的,Laravel 5 允许 file downloadsresponse()-&gt;download() 返回文件以供下载。我们不再需要弄乱任何标题。要返回一个文件,我们只需:

    return response()->download(public_path('file_path/from_public_dir.pdf'));
    

    从控制器内部。


    可重复使用的下载路由/控制器

    现在让我们创建一个可重用的文件下载路由和控制器,这样我们就可以在 public/files 目录中提供任何文件。

    创建控制器:

    php artisan make:controller --plain DownloadsController
    

    app/Http/routes.php中创建路由:

    Route::get('/download/{file}', 'DownloadsController@download');
    

    app/Http/Controllers/DownloadsController中制作下载方法:

    class DownloadsController extends Controller
    {
      public function download($file_name) {
        $file_path = public_path('files/'.$file_name);
        return response()->download($file_path);
      }
    }
    

    现在只需将一些文件放到public/files 目录中,您就可以通过链接到/download/filename.ext 来提供它们:

    <a href="/download/filename.ext">File Name</a> // update to your own "filename.ext"
    

    如果您拉入Laravel Collective's Html package,您可以使用 Html 外观:

    {!! Html::link('download/filename.ext', 'File Name') !!}
    

    【讨论】:

    • 工作就像一个魅力,非常感谢@DutGRIFF,你拯救了我的一天。我尝试了这个下载的东西 5 个小时,但没有成功,我没有得到任何解决方案,但是当我尝试你的解决方案时,它就像一个魅力。 Laravel 岩石。
    • 这是完美的代码!像迷人的一样工作!
    【解决方案3】:

    在接受的答案中,对于 Laravel 4,标头数组的构造不正确。使用:

    $headers = array(
      'Content-Type' => 'application/pdf',
    );
    

    【讨论】:

    • 数组实例化的差异可能是因为 PHP 版本,而不是 laravel 版本;)
    【解决方案4】:

    在使用 laravel 5 时,请使用此代码,因为您不需要标题。

    return response()-&gt;download($pathToFile);.

    如果您使用Fileentry,您可以使用以下功能进行下载。

    // download file
    public function download($fileId){  
        $entry = Fileentry::where('file_id', '=', $fileId)->firstOrFail();
        $pathToFile=storage_path()."/app/".$entry->filename;
        return response()->download($pathToFile);           
    }
    

    【讨论】:

    • 如果这个答案省略了不必要的部分,甚至只是在纯答案下方扩展它们,将会很有帮助。 Fileentry 是这个问题不需要的不同功能。编辑答案,我会投票,因为提到了 LV5s response()-&gt;download()
    【解决方案5】:

    其中不少解决方案建议引用 Laravel 应用程序的 public_path() 来定位文件。有时您会想要控制对文件的访问或提供对文件的实时监控。在这种情况下,您需要保持目录私有并通过控制器类中的方法限制访问。以下方法应该对此有所帮助:

    public function show(Request $request, File $file) {
    
        // Perform validation/authentication/auditing logic on the request
    
        // Fire off any events or notifiations (if applicable)
    
        return response()->download(storage_path('app/' . $file->location));
    }
    

    您也可以使用其他路径,详见 Laravel's helper functions documentation

    【讨论】:

    • 感谢您的贡献。我收到了 Http 500 错误,但这对我的情况有效。
    【解决方案6】:

    HTML href 链接点击:

    <a ="{{ route('download',$name->file) }}"> Download  </a>
    

    在控制器中:

    public function download($file){
        $file_path = public_path('uploads/cv/'.$file);
        return response()->download( $file_path);
    }
    

    在路线中:

    Route::get('/download/{file}','Controller@download')->name('download');
    

    【讨论】:

      【解决方案7】:

      我认为你可以使用

      $file= public_path(). "/download/info.pdf";
      
      $headers = array(
              'Content-Type: ' . mime_content_type( $file ),
          );
      

      有了这个,你可以确定这是一个 pdf。

      【讨论】:

        【解决方案8】:

        // 试试这个来下载任何文件。 laravel 5.*

        // 你需要使用外观 "use Illuminate\Http\Response;"

        public function getDownload()
        {
        
        //PDF file is stored under project/public/download/info.pdf
        
            $file= public_path(). "/download/info.pdf";   
        
            return response()->download($file);
        }
        

        【讨论】:

          【解决方案9】:
           HTML link click 
          <a class="download" href="{{route('project.download',$post->id)}}">DOWNLOAD</a>
          
          
          // Route
          
          Route::group(['middleware'=>['auth']], function(){
              Route::get('file-download/{id}', 'PostController@downloadproject')->name('project.download');
          });
          
          public function downloadproject($id) {
          
                  $book_cover = Post::where('id', $id)->firstOrFail();
                  $path = public_path(). '/storage/uploads/zip/'. $book_cover->zip;
                  return response()->download($path, $book_cover
                      ->original_filename, ['Content-Type' => $book_cover->mime]);
          
              }
          

          【讨论】:

            【解决方案10】:

            这是html部分

             <a href="{{route('download',$details->report_id)}}" type="button" class="btn btn-primary download" data-report_id="{{$details->report_id}}" >Download</a>
            

            这是路线:

            Route::get('/download/{id}', 'users\UserController@getDownload')->name('download')->middleware('auth');
            

            这是函数:

            public function getDownload(Request $request,$id)
            {
                            $file= public_path(). "/pdf/";  //path of your directory
                            $headers = array(
                                'Content-Type: application/pdf',
                            );
                             return Response::download($file.$pdfName, 'filename.pdf', $headers);      
            }
            

            【讨论】:

              【解决方案11】:

              您可以简单地在控制器内部使用: return response()-&gt;download($filePath); 快乐编码:)

              【讨论】:

                【解决方案12】:

                如果你想使用 JavaScript 下载功能,那么你也可以这样做

                 <a onclick="window.open('info.pdf) class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a>
                

                还记得将 info.pdf 文件粘贴到项目的公共目录中

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2013-01-18
                  • 2016-01-02
                  • 2018-11-17
                  • 1970-01-01
                  • 2014-10-16
                  • 2015-05-31
                  • 2017-12-11
                  • 2014-10-24
                  相关资源
                  最近更新 更多