【问题标题】:How can I download a document using Laravel?如何使用 Laravel 下载文档?
【发布时间】:2021-02-05 12:57:10
【问题描述】:

我需要从我的数据库中下载一个文档,我的文件在 storage/app/manual 上

在我的控制器上

  protected function downloadFile($src)
{
    if(is_file($src))
    {
        $finfo=finfo_open(FILEINFO_MIME_TYPE);
        $content_type=finfo_file($finfo,$src);
       finfo_open($finfo);
       $file_name=basename($src).PHP_EOL;
       $size=filesize($src);
       header("Content-Type: $content_type");
       header("Content-Disposition: attachment; filename=$file_name");
       header("Content-Transfer-Encoding: binary");
       header("Content-Length: $size");
       
        return true;
    }
    else
    {
        return false;
    }
}
public function download(Request $request)
{
    $note=$request->file('manual')->store('/','manual');
    if(!$this->downloadFile(app_path(). $note))
    {
        return redirect("/machineIndex");
       
    }
}

正在观看

                <td><a name="Manual" href="{{url('user/download')}}">Manual</a></td>

网络

Route::get('/user/download','MachineController@download')->name('MachineController@download');

Laravel 返回 Error 404 not found 或 Call to a member function store() on null

【问题讨论】:

    标签: php html laravel download


    【解决方案1】:

    您必须在控制器中返回下载响应:

    return Response::download($yourFilePath, $yourFileName);
    

    在您的&lt;a name="Manual" href="{{url('user/download')}}" download="name.png"&gt;Manual&lt;/a&gt;中添加下载名称

    【讨论】:

    • 但是我需要从数据库中获取文件,如果我添加一个名称,它将返回一个错误的文件。并不总是同一个文件。
    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 2020-06-12
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多