简体   繁体   中英

How to download the uploaded file in laravel

I want to make a function to download the uploaded file in storage but it always display an error code : The file "D:\\xampp php 7.2.2\\htdocs\\siapKerja_laravel\\public\\files/22" does not exist

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

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

You need to provide the full url of the file.

Try to add the file extension:

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

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

When saving the file you can also get the extension when storing it:

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

Try This Code

use Illuminate\Http\Request;

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

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

I hope it helps you. I uploaded file in "files" folder, not in storage folder. you just make your change according to that.set your content_types according to your uploaded file.

my controller code is

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));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM