Linked Questions

Popular Questions

html <img src=...> works but JS Image loading cause CORS error

Asked by At

I want to create picture editor in js+jquery. At the first step i ask user to give image url. But I come across problem when i try load image data inside JS (to generate base64 image uri). I get error in console: … has beeb blocked by CORS policy: Access-Control-Allow-Origin …. But I wonder why? If in html file i create for instance (image hotlink):

<img  src="" />

The browser load image without any CORS problems ! Here is my JS code which for the same image throw CORS problem:

 function downloadFile(url) {
    var img = new Image();
    img.onload = function() {
        // never execute because cors error
        // … make base64 uri with image data needed for further processing

    img.crossOrigin = "Anonymous";
    img.src = url;

So the question is - how to force JS to load image (as html-tag load it) and convert it to base64 url avoiding CORS problem?

Related Questions