尝试从 public 文件夹下载 PDF 文件时如何修复错误 404

How to fix Error 404 when trying to download PDF file from the public folder

尝试下载已成功保存的 pdf 文件时收到 404 错误消息。

索引

<tbody>
    @foreach ($allData as $key => $patient)
    <tr>
        <td>{{ $patient -> card_number }}</td>
        <td>{{ $patient -> patient_type }}</td>
        <td>{{ $patient -> patient_hmo }}</td>
        <td>{{ $patient -> patient_firstname }} {{ $patient -> patient_lastname }}</td>
        {{-- <td>{{ \Carbon\Carbon::parse($patient -> patient_dob)->diff(\Carbon\Carbon::now())->format('%y years, %m months and %d days') }}</td> --}}
        <td>{{\Carbon\Carbon::parse($patient -> patient_dob)->diff(\Carbon\Carbon::now())->format('%y')  }}</td>
        <td>{{ $patient -> patient_phone }}</td>
        <td>{{ $patient -> patient_gender }}</td>
        <td>{{ $patient -> patient_kin_name }}</td>
        <td>{{ $patient -> patient_kin_phone }}</td>
        <td><a href="/file/download/{{ $patient->file }}" target="blank">Download</a></td>
        <td>


            <a class="btn btn-primary btn-rounded btn-sm" title="View Data" data-toggle="modal" data-target="#modal-center{{ $patient->id }}"> <i class="fa fa-eye-slash"></i></a>

            <a href="{{ route('patient.edit',$patient->id) }}" class="btn btn-info btn-rounded btn-sm" title="Edit Data" > <i class="fa fa-edit"></i></a>

            <a href="{{ route('patient.delete',$patient->id) }}"  class="btn btn-danger btn-rounded btn-sm"  id="delete" title="Delete Data" > <i class="fa fa-trash"></i></a>
        </td>

    </tr>
    @endforeach

</tbody>

网络

Route::get('/file/download/{file}',[PatientController::class,'downloads']);

患者控制器

public function downloads($file)
{
return response()->download('upload/pdf'.$file);
}

数据库

错误页面

This works directly without Route or any controller.


<tbody>
    @foreach ($allData as $key => $patient)
    <tr>
        <td>{{ $patient -> card_number }}</td>
        <td>{{ $patient -> patient_type }}</td>
        <td>{{ $patient -> patient_hmo }}</td>
        <td>{{ $patient -> patient_firstname }} {{ $patient -> patient_lastname }}</td>
        <td>{{\Carbon\Carbon::parse($patient -> patient_dob)->diff(\Carbon\Carbon::now())->format('%y')  }}</td>
        <td>{{ $patient -> patient_phone }}</td>
        <td>{{ $patient -> patient_gender }}</td>
        <td>{{ $patient -> patient_kin_name }}</td>
        <td>{{ $patient -> patient_kin_phone }}</td>

        <td><a href="{{ url('/'.$patient->file) }}">Download</a></td>
        <td>


            <a class="btn btn-primary btn-rounded btn-sm" title="View Data" data-toggle="modal" data-target="#modal-center{{ $patient->id }}"> <i class="fa fa-eye-slash"></i></a>

            <a href="{{ route('patient.edit',$patient->id) }}" class="btn btn-info btn-rounded btn-sm" title="Edit Data" > <i class="fa fa-edit"></i></a>

            <a href="{{ route('patient.delete',$patient->id) }}"  class="btn btn-danger btn-rounded btn-sm"  id="delete" title="Delete Data" > <i class="fa fa-trash"></i></a>
        </td>

    </tr>
    @endforeach

</tbody>