【问题标题】:Download files using Laravel's Storage facade使用 Laravel Storage 门面下载文件
【发布时间】:2017-10-21 23:05:37
【问题描述】:

我在storage/app/ 目录中有一个目录dl,我设法使用Storage::get(dl/filename.ext) 获取文件内容
但现在我想为这些文件创建一个下载链接,但我对 Response 和 Storage 外观如何工作感到有些困惑。

我正在使用带有这样定义路由的 Response 外观:

Route::get('site/download/{fileName}', function($fileName) {
    $path = storage_path().'/app/dl/'.$fileName;

    return Response::download($path, $fileName);
});

注意:我使用的是 Laragon,站点是根目录。
发生的问题是 Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?) 。我知道这是一个已知问题,但在寻找解决方案后,我没有找到适合我需要的东西。这些文件是 C 文件,mime 类型可能应该是“文本/纯文本”,但我不知道如何以及在哪里指定它。
我还寻找其他解决方案,但file does not exists 不断出现。
我需要一些东西来从storage/app/dl/ 下载文件。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您的路径包含文件名,因此“文件不存在”

    这对我有用:

    Route::get('test', function() {
    
       $pathToFile = storage_path('app/test.txt');
    
       return response()->download($pathToFile);
    
    });
    

    【讨论】:

    • 它实际上缩短了我写它的方式,所以它更清楚......这很好......但它仍然告诉我一些 mime 类型错误,这是一些 php_fileinfo 神秘向导无法猜到的....
    猜你喜欢
    • 2023-04-11
    • 2015-05-11
    • 2021-11-11
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2016-01-15
    • 2021-01-18
    相关资源
    最近更新 更多