Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

266
Visualizações
React js: No 'Access-Control-Allow-Origin' header is present on the requested resource while using htmlToImage

I am trying to use htmlToImage method but is showing the error "Access to fetch at " / aws image url / " from origin 'http://localhost:3000' 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

AllowedOrigins as "". AllowedHeaders as "".

This is my code.

import React, { Component } from 'react'
import * as htmlToImage from "html-to-image";

export default class DownloadableImage extends Component {
constructor(props) {
    super(props)

    this.state = {
   
    }
  }
 handleDownloadImage = ()=>{
  const post = document.getElementById("generateImage");
    htmlToImage
      .toJpeg(post, {
        style: {
          backgroundColor: "#fff",
        },
        quality: 0.95,
      })
      .then(function (dataUrl) {
        var link = document.createElement("a");
        link.download = "download.jpeg";
        link.href = dataUrl;
        link.click();
      })
      .catch((error) => console.log(error, "error"));
}
  render() {
    return (  
        <div id="generateImage">
            <img class="img-style" src="imgUrl" alt="download_image"> 
        <div>
        <button onClick={this.handleDownloadImage}>Download</button>
    )
  }
}

Community, Please help me out.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You have to add cors configuration to your bucket properties.

Refer to this thread for more details: S3 - Access-Control-Allow-Origin Header

about 4 years ago · Santiago Trujillo Relatório

0

there is a checklist for you.

1、If you want to edit an image, you should add the crossOrigin attribute to the img node.

2、Note, In My case. I got the cached image, and the image request doesn't send the cross origin header. So it will get an error.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda