如何预览 public 文件夹 Laravel 5.4 中的文件

How to preview files from public folder Laravel 5.4

您好,我正在尝试预览 public 文件夹中的文件,以便用户可以确保这是他们要删除的文件。

这是我当前的代码:

HTML

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

控制器

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 . '"',
    ));
}

它不工作。它没有显示文件的预览,而是提示我下载文件。请帮忙。

文件类型包括word文档、excel、ppt、pdf、图片。

编辑:

我试过上传图片和pdf文件,都可以。但是在 word 文档上它不起作用

如前所述 here :

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

截至目前,所有最新的浏览器都不支持 YET 。也许在不久的将来,它会。

其他选择是让用户检查文件的其他详细信息,例如标题、类型、日期或其他任何内容。也许你在悬停事件上做了一些功能并向他们展示了细节。