[Vuejs]-Vue.js Get public folder images in laravel 5.8

1👍

My solution is create a api route to show image. For example.

api.php

Route::get('profile/{fileName}', 'ProfileController@showImage');

ProfileController.php

class ProfileController {
    ...
    public function showImage($fileName)
    {
        $path = public_path("/images/{$fileName}");

        if (!\File::exists($path)) {
            return response()->json(['message' => 'Image not found.'], 404);
        }

        $file = \File::get($path);
        $type = \File::mimeType($path);

        $response = \Response::make($file, 200);
        $response->header("Content-Type", $type);

        return $response;
    }

}

And you image src will be /api/profile/{imageName}. This is my solution.

Leave a comment