我的问题是,当html2canvas尝试渲染一个图像时,它无法告诉我cors政策存在问题。我检查了请求,发现响应标头中没有access-control-allow-origin。
我在Startap.cs和api的Configure()方法中添加了以下代码:
app.Use((context,next) =>
{
context.Response.Headers["access-control-allow-origin"] = "*";
return next.Invoke();
});
上面的代码将access-control-allow-origin添加到所有响应标头,但HTML2canvas尝试获取图像时除外。
提醒,仅当html2cavas尝试从api获取图像时,我才发现cors存在此问题。
其他一切正常。
有人可以帮忙吗?
这是前端代码:
html2canvas(data,{logging: true,allowTaint: false,useCORS: true }).then(canvas => {
// var imgWidth = 220;
// var pageHeight = 320;
var imgWidth = 280; // valor anterior: 280
var pageHeight = 1000; //valor anterior: 1000
var imgHeight = canvas.height * imgWidth / canvas.width;
//var imgHeight = 2000;
var heightLeft = imgHeight;
var widthLeft = imgWidth;
const contentDataURL = canvas.toDataURL('image/png');
let pdf = new jspdf('landscape','mm','a4',true); // A4 size page of PDF
var position = 0;
pdf.addImage(contentDataURL,'PNG',position,imgWidth,imgHeight,'','FAST');
pdf.save('PadronTecnico.pdf'); // Generated PDF
this.blockUI.stop();
this.showScrolls = true;
})