Laravel:文件下载

问题描述 投票:0回答:4

我有一个这样的下载脚本:

    public function downloadSampleCSV(){
        $file = public_path()."/downloads/Payout example file.csv";
        $headers = array('Content-Type: text/csv',);
        return Response::download($file, 'Payout example file.csv',$headers);
    }

文件路径是正确的,网络面板甚至输出了文件的内容,但文件实际上并没有从浏览器下载。

这是网络面板显示的内容。您可以知道它已找到该文件,但只是不继续下载它:

可能是什么问题?

laravel download
4个回答
0
投票

允许使用response()->download()返回文件进行下载。我们不再需要弄乱任何标头。要返回文件,我们只需:

return response()->download(public_path('/downloads/Payoutexamplefile.csv'));

0
投票

解决方案与 Orchid 模板本身及其处理 AJAX 的方式有关。您应该将

->turbo(false)
添加到按钮。

所以,按钮应该是这样的:

Button::make('Download Template ')
 ->method('downloadSampleCSV')
 ->turbo(false),

0
投票

我在使用Livewire时遇到了这个问题。为了解决这个问题我改变了我的方法。我制作了一个 Laravel 控制器和一条路线,然后在我的 Blade 文件中请求了这条路线。

控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class FileDownload extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        $fileName = $request->fileName;
        $filePath = public_path() . "/downloads/" . $fileName;
        return Storage::disk('local')->download($filePath);       
    }
}

路线:

Route::get('/file/download/{fileName}', App\Http\Controllers\FileDownload::class)->name('file-download');

刀片:

<a href="{{ route('file-download', ['fileName' => 'Your file name can be loaded dynamically' }}">Download</a>

-2
投票

为什么你不在刀片文件中使用它?

<a href="{{ asset('/downloads/Payout example file.csv') }}" download> Download File </a>

© www.soinside.com 2019 - 2024. All rights reserved.