Home > Software design >  How to open file stored in stoarge folder in laravel?
How to open file stored in stoarge folder in laravel?

Time:12-23

I am trying to open files which is stored in "storage/app/public/cca_license/pdf_upload" folder. When I try to open the file, the content is empty.
What I have tried is

account.blade.php

 <table id="table" >
            <thead>
                <tr>
                    <th>PDF Name </th>
                    <th>Date</th>
                   
                    <th>View</th><!-- comment -->
                    <th>Delete</th>
                </tr>
            </thead>
            <tbody>
                @if(count($pdf_data)> 0)
                @foreach($pdf_data as $data)
            <tr>
                <td>{{$data->pdf_name}}</td>
                <td>{{$data->pdf_date}}</td>
                <td>
                    <a href="/openPdf/{{$data->pdf_name}}"><i ></i></a>
                   <!-- <iframe id="ifrm" src="/cca_license/pdf_upload/txtfile-converted_1640170538.pdf" >
                   </iframe>-->
                  </td>
                  <td><a href="/deleteFile/{{$data->id}}"><i ></i> </a></td>
            </tr>
            @endforeach
            
           @else
           <tr><td colspan="4">No Pdf Listings</td></tr>
            @endif
            </tbody>
        </table>

web.php

Route::get('/openPdf/{name}', 'HomeController@openPdf');

HomeController.php

public function openPdf($name) {

    $contents=asset('cca_license/pdf_upload/'. $name);
  
}

When I try to open a file, i got blank white page without file contents.

Output

How to view contents along with file in browser.

CodePudding user response:

Your code does not contain any return statement. Laravel offers a variety of ways to make responses.

https://laravel.com/docs/8.x/responses

For example, a file response.

public function openPdf($name) 
{
    return response()->file(storage_path('app/public/cca_license/pdf_upload/'. $name));
}

The asset() helper returns a URL. If the file is stored locally, you should refer to a local path.

  • Related