Cors Origin in Direct Path in laravel when calling images

now i am creating a web app using laravel 5.2 as webserivce and angular2 as homepage i am using VR Library from http://cdn.pannellum.org My Question When I call Iamge link from my database like mywebsite.com/ public / Images / photo7.png return me this message

Heading

'Access-Control-Allow-Origin' is present on the requested resource. Origin ' http: // localhost: 5565 ' is therefore not allowed access.

but I handled this issue before starting a data transfer project from laravel and angular, but when I call my images from direct path, it gives me back this message

+3


source to share


1 answer


Laravel 5 (L5) . In my project, when I return an image (file) and want to avoid CORS, I use the following code (I change it a bit by hand without testing, but it should work):

private function responseFile($filePath, $fileMimeType,  $originalFileName) {

    $headers = array(
        'Content-Type' => $fileMimeType,
        //'Content-Disposition' => 'attachment; filename="'. $attachment->org_name . '"', - this header is added automaticaly
        "Access-Control-Allow-Origin" => request()->getSchemeAndHttpHost() // allow CORS (e.g. for images)
    );

    return response()->download($filePath, $originalFileName, $headers);
}

      



In this solution, you don't need to touch the apache .htaccess file (or nginx config files) at all, because in this case we are using a so called Simple CORS request .

You can also read this answer to avoid other issues that could cause a CORS error.

0


source







All Articles