【问题标题】:How to download the uploaded file in laravel如何在laravel中下载上传的文件
【发布时间】:2019-09-18 07:05:03
【问题描述】:

我想创建一个函数来下载存储中的上传文件,但它总是显示错误代码:文件“D:\xampp php 7.2.2\htdocs\siapKerja_laravel\public\files/22”不存在

public function show($idFile)
 { 
    $file = public_path(). "/files/";
    return Response()->download($file.$idFile);$
 } 

Route::get('/verifikasi/pkwt/download/{id}', 'FileController@show')->name('downloadFile');

【问题讨论】:

  • 你有什么问题吗?
  • 提及您的问题,而不是要求

标签: php laravel file


【解决方案1】:

您需要提供文件的完整网址。

尝试添加文件扩展名:

$extension = ".png" // or whatever type file is

return Response()->download($file.$idFile.$extension);

保存文件时也可以获取扩展名:

$extension = $request->file('file')->extension();

【讨论】:

    【解决方案2】:

    希望对你有帮助。我在“文件”文件夹中上传了文件,而不是在存储文件夹中。您只需根据 that.set 您的 content_types 根据您的 上传文件。

    我的控制器代码是

    public function  openPdf(Request $request,$docFile)
    {   
    
        $file='/var/www/html/laravelQuiz/public/files/'.$docFile;
         $extension = explode(".",$docFile);
    
         foreach($extension as $extensions){
            if($extensions == 'pdf'){
               $content_types =  'application/pdf';
            }elseif($extensions == 'doc'){
               $content_types =  'application/msword';
            }elseif($extensions == 'txt'){
                $content_types =   'application/octet-stream';
            }elseif($extensions == 'docx'){
                $content_types =     'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
            }elseif($extensions == 'ppt'){
                $content_types = 'application/vnd.ms-powerpoint';
            }elseif($extensions == 'odt'){
                $content_types = 'application/vnd.ms-powerpoint';
            }elseif($extensions == 'txt'){
                $content_types = 'text/plain';
            }
         }
         $content = file_get_contents($file);               
         return Response::make($content, 200, array('content-type'=>$content_types));
    
    }
    

    【讨论】:

    • shraddha public_path() 将给出直到'/var/www/html/laravelQuiz/public/的路径
    猜你喜欢
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2017-06-16
    • 2017-03-12
    • 2017-06-03
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    相关资源
    最近更新 更多