如何定义命名路由并从laravel中的数据库下载文件

时间:2018-07-31 06:19:02

标签: laravel-5

我在刀片中定义的网址为:

href="/finance/invoices/download/{{ $invoice->file_path }}"

,而web.php中的路由为:

Route::get('invoices/download/{year}/{month}/{file}', 'InvoiceController@download');

我的文件在数据库中的存储方式为:

2018/07/invoiceberry_invoice_template_1.docx

控制器中的编辑功能是:

public function edit(Invoice $invoice)
{
    $projectStageBillings = $invoice->projectStageBillings;

    $projectStageBilling = $projectStageBillings->first();
    $client = $projectStageBilling->projectStage->project->client;
    $client->load('projects', 'projects.stages', 'projects.stages.billings');

    $billings = [];
    foreach ($projectStageBillings as $key => $billing) {
        $billing->load('projectStage', 'projectStage.project');
        $billings[] = $billing;
    }

    return view('finance.invoice.edit')->with([
        'invoice' => $invoice,
        'clients' => Client::select('id', 'name')->get(),
        'invoice_client' => $client,
        'invoice_billings' => $billings,

    ]);
}

下载方法为:

public function download($year, $month, $file, $inline = true)
{
    $headers = [
        'content-type' => 'application/pdf',
    ];

    $file_path = FileHelper::getFilePath($year, $month, $file);

    if (!$file_path) {
        return false;
    }

    if ($inline) {
        return Response::make(Storage::get($file_path), 200, $headers);
    }

    return Storage::download($file_path);
}

如何获取文件的网址并下载?

0 个答案:

没有答案