Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

javascript - No 'Access-Control-Allow-Origin' header is present on the requested resource while using htmlToImage

I am trying to use htmlToImage method but is is showing error "Access to fetch at " / aws image url / " from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."

The image url which I am using in img tag, is coming from aws s3 bucket. And on AWS s3 we have already configured CORS

  1. AllowedOrigins as "*".
  2. AllowedHeaders as "*".

Note:

  1. This is working fine in firefox browser but not working in chrome browser and Opera browser.
  2. When I am opening genrated base64 in browser tab, the image is not coming. only blank is coming.

My html code is :

<div id="editedimage">
   <img class="img-style" src="<imgUrl>" alt="image"> 
<div>

And js code is :

    var node = document.getElementById('editedimage');
    htmlToImage.toPng(node)
      .then((dataUrl) => {
        console.log("url", dataUrl);
      .catch((error) => {
      console.error("error", error);
      });

Team, Help me out. Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to display base 64 image you need to sanitize the url before using it in template.

import { DomSanitizer } from '@angular/platform-browser';

constructor( private sanitizer: DomSanitizer, .... ) {
    this.displayImage = this.sanitizer.bypassSecurityTrustUrl("data:Image/*;base64,"+imageData);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...