Skip to content
Advertisement

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

Have been receiving a 404 error message when trying to download a pdf file that was successfully saved.

Index

<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>{{ CarbonCarbon::parse($patient -> patient_dob)->diff(CarbonCarbon::now())->format('%y years, %m months and %d days') }}</td> --}}
        <td>{{CarbonCarbon::parse($patient -> patient_dob)->diff(CarbonCarbon::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>

web

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

PatientController

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

Database

enter image description here

Error page

enter image description here

Advertisement

Answer

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>{{CarbonCarbon::parse($patient -> patient_dob)->diff(CarbonCarbon::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>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement