使用 Response::d ownload 在 laravel 中下载文件


Download files in laravel using Response::download

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

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

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

以下是我试图实现的目标:

按钮:

  <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);
}

试试这个。

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"将不起作用,因为您必须提供完整的物理路径。

更新 20/05/2016

Laravel 5、5.1、5.2 或 5.* 用户可以使用以下方法代替Response外观。但是,我之前的答案将适用于 Laravel 4 或 5。($header数组结构更改为关联数组=> - 删除"内容类型"后的冒号 - 如果我们不进行这些更改,那么标题将以错误的方式添加:标头的名称将从 0,1,... 开始(

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

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

正如@Ashwani提到的,Laravel 5允许使用response()->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的Html包,你可以使用Html外观:

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

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

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

其中相当多的解决方案建议引用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 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);           
}
我认为

您可以使用

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

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

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');

//试试这个来下载任何文件。 拉拉维尔 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);
}
 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]);
    }
这是

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);      
}

您只需在控制器中使用: return response()->download($filePath);快乐的编码:)

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

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

另请记住将 info.pdf 文件粘贴到项目的公共目录中