Cors Origin in Direct Path in laravel when calling images Cors Origin in Direct Path in laravel when calling images laravel laravel

Cors Origin in Direct Path in laravel when calling images


Laravel 5 (L5): In my project in, when I return image (file) and want to avoid CORS i use following code (I change it a little 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 apache .htaccess file (or nginx config files) at all (if you do it it will cause errors with headers duplication) - because in this case we use so called Simple CORS request.

You can also read this answer to avoid other problems which can cause that CORS error.


Try these methods:

  1. Install and config Cors from enter link description here
  2. Add Cors mode
fetch(url,{mode:"cors"})                    .then(res => res.blob())                    .then(blob => {                        const file = new File([blob], 'dot.png', {type:'image/png'});                        console.log(file);                    });
  1. Resource from enter link description here