Paul Ryan Lucero
Paul Ryan Lucero

Reputation: 551

How to preview files from public folder Laravel 5.4

Hello I'm trying to preview files from public folder so users can make sure it is the file they are trying to delete.

Here is my current code:

HTML

<a href="{{ route('preview_attachment', $material->filename) }}" class="btn btn-warning btn-sm">
    Preview
</a>

Controller

public function previewAttachment($filename) {
    $file = public_path('training/attachments/' . $filename);

    return Response::make(file_get_contents($file), 200, array(
        'Content-Type' => File::mimeType($file),
        'Content-Disposition' => 'inline; filename="' . $filename . '"',
    ));
}

It is not working. Instead of showing a preview of the file, it prompts me to download the file. Any help please.

The filetypes range from word docs, excel, ppt, pdf, images.

Edit :

I have tried uploading images and pdf files, and it is working. however on word docs it is not working

Upvotes: 1

Views: 1779

Answers (2)

Coringa
Coringa

Reputation: 1

public function previewAttachment($filename) {
    $file = public_path('training/attachments/' . $filename);

    return Response::make(file_get_contents($file), 200, array(
        'Content-Type' => File::mimeType($file),
        'Content-Disposition' => 'inline; filename="' . $filename . '"',
    ));
}

try doing this on your blade template my firend:

<iframe src="{{asset('training/attachments/'.$filename)}}"></iframe>

Upvotes: 0

Romnick Susa
Romnick Susa

Reputation: 1288

As stated here :

No browsers currently have the code necessary to render Word Documents.

As of now, all latest browser does not support YET . Maybe in the near future, it will.

Other alternatives is to let the user check the other details of the file such us Title, Type, Date or whatever. Maybe you make some function on hover event and show them details.

Upvotes: 1

Related Questions