Laravel - display a PDF file in storage without forcing download?

后端 未结 10 998
陌清茗
陌清茗 2020-12-02 10:31

I have a PDF file stored in app/storage/, and I want authenticated users to be able to view this file. I know that I can make them download it using

return          


        
相关标签:
10条回答
  • 2020-12-02 11:12

    Ben Swinburne answer was so helpful.

    The code below is for those who have their PDF file in database like me.

    $pdf = DB::table('exportfiles')->select('pdf')->where('user_id', $user_id)->first();
    
    return Response::make(base64_decode( $pdf->pdf), 200, [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; filename="'.$filename.'"',
    ]);
    

    Where $pdf->pdf is the file column in database.

    0 讨论(0)
  • 2020-12-02 11:15

    I am using Laravel 5.4 and response()->file('path/to/file.ext') to open e.g. a pdf in inline-mode in browsers. This works quite well, but when a user wants to save the file, the save-dialog suggests the last part of the url as filename.

    I already tried adding a headers-array like mentioned in the Laravel-docs, but this doesn't seem to override the header set by the file()-method:

    return response()->file('path/to/file.ext', [
      'Content-Disposition' => 'inline; filename="'. $fileNameFromDb .'"'
    ]);
    
    0 讨论(0)
  • 2020-12-02 11:15

    Laravel 5.6.*

    $name = 'file.jpg';
    

    store on image or pdf

    $file->storeAs('public/', $name );
    

    download image or pdf

    return response()->download($name);
    

    view image or pdf

    return response()->file($name);
    
    0 讨论(0)
  • 2020-12-02 11:21

    Retrieve File name first then in Blade file use anchor(a) tag like below shown. This would works for image view also.

    <a href="{{ asset('storage/admission-document-uploads/' . $filename) }}" target="_black"> view Pdf </a>;

    0 讨论(0)
提交回复
热议问题