使用Response :: download下载laravel中的文件

sta*_*bit 60 php laravel laravel-4 laravel-routing

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

The file "/public/download/info.pdf" does not exist
Run Code Online (Sandbox Code Playgroud)

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

以下是我试图实现的方法:

按钮:

  <a href="/download" class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a>
Run Code Online (Sandbox Code Playgroud)

路线:

Route::get('/download', 'HomeController@getDownload');
Run Code Online (Sandbox Code Playgroud)

控制器:

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

Ana*_*nam 138

试试这个.

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);
}
Run Code Online (Sandbox Code Playgroud)

"./download/info.pdf"不会工作,因为你必须提供完整的物理路径.

2016年5月20日更新

Laravel 5,5.1,5.2或5.*用户可以使用以下方法代替Response外观.但是,我之前的答案对Laravel 4或5都有效.($header数组结构更改为关联数组=>- 删除'Content-Type'后的冒号 - 如果我们不进行这些更改,则会以错误的方式添加标题:标题的名称将是从0,1开始的数字,...)

$headers = [
              'Content-Type' => 'application/pdf',
           ];

return response()->download($file, 'filename.pdf', $headers);
Run Code Online (Sandbox Code Playgroud)

  • @SazzadTusharKhan只使用`return Response :: download($ pathToFile);` (4认同)

Dut*_*IFF 31

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

作为@Ashwani提到Laravel 5允许文件下载response()->download()恢复文件下载.我们不再需要弄乱任何标题.要返回文件,我们只需:

return response()->download(public_path('file_path/from_public_dir.pdf'));
Run Code Online (Sandbox Code Playgroud)

从控制器内部.


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

现在让我们创建一个可重用的文件下载路由和控制器,以便我们可以为我们public/files目录中的任何文件提供服务.

创建控制器:

php artisan make:controller --plain DownloadsController
Run Code Online (Sandbox Code Playgroud)

app/Http/routes.php以下位置创建路线:

Route::get('/download/{file}', 'DownloadsController@download');
Run Code Online (Sandbox Code Playgroud)

制作下载方法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);
  }
}
Run Code Online (Sandbox Code Playgroud)

现在只需删除目录中的一些文件,public/files然后通过链接到/download/filename.ext以下内容来提供服务:

<a href="/download/filename.ext">File Name</a> // update to your own "filename.ext"
Run Code Online (Sandbox Code Playgroud)

如果您使用Laravel Collective的Html包,您可以使用Html外观:

{!! Html::link('download/filename.ext', 'File Name') !!}
Run Code Online (Sandbox Code Playgroud)


seb*_*ebt 26

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

$headers = array(
  'Content-Type' => 'application/pdf',
);
Run Code Online (Sandbox Code Playgroud)


小智 8

我认为你可以使用

$file= public_path(). "/download/info.pdf";

$headers = array(
        'Content-Type: ' . mime_content_type( $file ),
    );
Run Code Online (Sandbox Code Playgroud)

有了这个你就可以确定这是一个pdf文件。


小智 8

HTML href 链接点击:

<a ="{{ route('download',$name->file) }}"> Download  </a>
Run Code Online (Sandbox Code Playgroud)

在控制器中:

<a ="{{ route('download',$name->file) }}"> Download  </a>
Run Code Online (Sandbox Code Playgroud)

路线中:

Route::get('/download/{file}','Controller@download')->name('download');
Run Code Online (Sandbox Code Playgroud)


Ash*_*war 5

在使用laravel 5使用此代码,你不`吨需要头。

return response()->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);           
}
Run Code Online (Sandbox Code Playgroud)

  • 如果它省略了不必要的部分,或者甚至只是在纯粹的答案下方扩展了它们,那么这个答案将会很有帮助。`Fileentry` 是这个问题不需要的不同功能。编辑答案,我会赞成,因为提到了 LV5 的 `response()-&gt;download()`。 (2认同)

Kir*_*and 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));
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用其他路径,如 Laravel的辅助函数文档中所述