Dhruv Koshiyar
Dhruv Koshiyar

Reputation: 167

Downloading File error in laravel

I have used local storage for file storing. I have created table of file, in which title, path, card_id rows stored.

$request->file('file')->storeAs('public/upload',$files->getClientOriginalName());

i.e:passport.png stored at /home/dhruv/MyProject/storage/app/public/upload/passport.png

Above works fine but I'm getting error while downloading file. in controller...

    public function getDownload($file_id)
    {

        $file = File::find($file_id);
        //print_r($file);    
            return response()->download($file->path);
             /*  return response()->download(
                $file->path,
                    $file->name,
                    [],
                    'inline'//attachment
                );*/

    }

I tried many downloading methods but any didn't work fine. I am getting this error.

Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: The file "/home/dhruv/MyProject/storage/public/upload/passport.png" does not exist in file /home/dhruv/MyProject/vendor/symfony/http-foundation/File/File.php on line 37

I dont know why it is searching at /home/dhruv/MyProject/vendor/symfony/http-foundation/File/File.php since i have inserted correct path.

So suggest any method for downloading ..

Upvotes: 2

Views: 3865

Answers (1)

TalESid
TalESid

Reputation: 2524

return response()->download(Storage::get($file->path));

Import this before:
use Illuminate\Support\Facades\Storage;

Laravel Storage

Or try this:
return response()->download(storage_path($file->path));

Upvotes: 0

Related Questions