简体   繁体   English

未捕获的 DOMException:无法在“HTMLCanvasElement”上执行“toDataURL”:可能无法导出受污染的画布

[英]Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported

Looks like the issue is related mostly to my aws s3 bucket CORS config看起来问题主要与我的 aws s3 存储桶 CORS 配置有关

I added:我补充说:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>https://www.example.com</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

to my bucket CORS config, but this didn't help either.到我的存储桶 CORS 配置,但这也无济于事。 For some reason I am able to add the photos to the canvas, (depending on the code) but not able to save the canvas with the image from AWS出于某种原因,我能够将照片添加到 canvas,(取决于代码)但无法使用来自 AWS 的图像保存 canvas


I have a fabric.js canvas that is being tainted on images from amazon s3.我有一个 fabric.js canvas,它被亚马逊 s3 的图像污染了。 I am unsure on exactly what is going on.我不确定到底发生了什么。

When I try to save the canvas by clicking save:当我尝试通过单击保存来保存 canvas 时:

Chrome Error on console: Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.控制台上的 Chrome 错误:未捕获的 DOMException:无法在“HTMLCanvasElement”上执行“toDataURL”:可能无法导出受污染的画布。

FireFox Error on console: Operation is insecure FireFox 控制台错误:操作不安全

https://jsfiddle.net/je3mL5go/1/ https://jsfiddle.net/je3mL5go/1/

In the jsfiddle, it works when the added line }, {crossOrigin: 'Anonymous'});在 jsfiddle 中,当添加行}, {crossOrigin: 'Anonymous'}); is present.存在。

In production on Heroku with s3 Images:在 Heroku 上生产,带有 s3 图像:

In FireFox, I can select an image from the select menu and have it add to the canvas.在 FireFox 中,我可以 select 中的 select 菜单中的图像并将其添加到 ZFCC790C72A86190DECF42Z 菜单中。 I'll get the error as mentioned but if I repeatedly click the "Save" button, after enough clicks (usually 5-30), it will allow me to save the canvas as a png.我会收到上面提到的错误,但如果我反复单击“保存”按钮,在足够的点击次数(通常是 5-30 次)之后,它将允许我将 canvas 保存为 png。

In Chrome, this "hack" doesn't seem to work at all.在 Chrome 中,这种“hack”似乎根本不起作用。

Is there an explanation behind this?这背后有解释吗?

It also seems that because I am hosting the images in AWS S3, this effects it as well.似乎因为我在 AWS S3 中托管图像,这也会影响它。 But even with this, the above example (repeat clicking in FireFox) still works.但即便如此,上述示例(在 FireFox 中重复单击)仍然有效。

Attempts:尝试:

With:和:

function updateTshirtImage(imageURL){
  fabric.Image.fromURL(imageURL, function(img) {
    img.scaleToHeight(300);
    img.scaleToWidth(300);
    img.crossOrigin = 'anonymous';
    canvas.centerObject(img);
    canvas.add(img);
    canvas.renderAll();
  });
};

I am able to upload the image but when i click save:我可以上传图片,但是当我点击保存时:

I get the same errors as above.我得到与上述相同的错误。

When i use:当我使用:

function updateTshirtImage(imageURL){
  fabric.Image.fromURL(imageURL, function(img) {
     img.scaleToHeight(300);
     img.scaleToWidth(300);
     canvas.centerObject(img);
     canvas.add(img);
     canvas.renderAll();
  }, {crossOrigin: 'anonymous'});
};

Error on upload by select image select 图像上传时出错

No 'Access-Control-Origin-Header' is present...

Attempt:试图:

function updateTshirtImage(imageURL){
   var rand = '?'+Math.random();
   var no_cors = new Image();
   no_cors.onload = loadCORS;
   no_cors.src = imageURL + rand;

   function loadCORS(){
     var with_cors = new Image();
     with_cors.crossOrigin = 'anonymous';
     with_cors.src = no_cors.src;
     with_cors.onload = function(){console.log('loaded');};
     with_cors.onerror = function(){console.error('failed');};
     fabric.Image.fromURL(with_cors.src, function(img) {
     img.scaleToHeight(300);
     img.scaleToWidth(300);
     canvas.centerObject(img);
     canvas.add(img);
     canvas.renderAll();
   }, {crossOrigin: 'anonymous'});
  }
};

var images = <%= images_2.to_h.to_json.html_safe %>
document.getElementById("tshirt-design").addEventListener("change", function(){
    updateTshirtImage(images[this.value]);
}, false);

error:错误:

Chrome: 404 error铬:404 错误

Firefox: No error, not working. Firefox:没有错误,不工作。

Is the issue here is that I'm somehow changing the url for aws s3 before asking for it from aws?这里的问题是我在向aws询问之前以某种方式更改了aws s3的url吗?

The adding of:的添加:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>https://www.example.com</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

Actually did the trick.确实做到了。 Looks like I just needed to wait.看来我只需要等待。 The next day I tested and it worked!第二天我测试并成功了!

To clarify, the above header code is probably overkill but this is where I am currently at.澄清一下,上面的 header 代码可能有点矫枉过正,但这是我目前所处的位置。 I will be testing and "debugging" and removing some Allowed Methods and see which and which are not needed.我将测试和“调试”并删除一些允许的方法,看看哪些是不需要的。

For anyone who still encountering the same issue even after applying the server cross-origin settings, it probably a browser caching issue.对于即使在应用服务器跨域设置后仍然遇到相同问题的任何人,这可能是浏览器缓存问题。 So you need to make sure to disable the caching and test again, you can do that from the browser dev-tools -> network tab -> click on disable cash option -> try again: Chrome dev tools因此,您需要确保禁用缓存并再次测试,您可以从浏览器开发工具 -> 网络选项卡 -> 单击禁用现金选项 -> 重试: Chrome 开发工具

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DOMException:无法在“HTMLCanvasElement”上执行“toDataURL”:可能无法导出受污染的画布 - DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported 为什么收到错误“未捕获的SecurityError:无法在&#39;HTMLCanvasElement&#39;上执行&#39;toDataURL&#39;:错误的画布可能无法导出。 ” - Why am I getting the error “Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported. ” 无法在“HTMLCanvasElement”上执行“toBlob”:受污染的画布可能无法在 react-image-crop 中导出 - Failed to execute 'toBlob' on 'HTMLCanvasElement': Tainted canvases may not be exported in react-image-crop Tessearct.js 不工作:无法在“HTMLCanvasElement”上执行“toBlob”:可能无法导出受污染的画布 - Tessearct.js not working: Failed to execute 'toBlob' on 'HTMLCanvasElement': Tainted canvases may not be exported 未捕获(承诺)DOMException:无法在“WebGL2RenderingContext”上执行“texImage2D”:可能无法加载受污染的画布 - Uncaught (in promise) DOMException: Failed to execute 'texImage2D' on 'WebGL2RenderingContext': Tainted canvases may not be loaded Canvas.toDataURL() 受污染的画布可能无法导出 - Canvas.toDataURL() Tainted canvases may not be exported JavaScript:toDataUrl()抛出“安全错误:可能无法导出受污染的画布”。 - JavaScript: toDataUrl() throwing “Security Error: Tainted canvases may not be exported.” 受污染的画布不得出口 - Tainted canvases may not be exported 未捕获的安全错误:无法在 'HTMLCanvasElement' [JS] [jQuery] 上执行 'toDataURL' - Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement' [JS] [jQuery] OpenLayers:&#39;无法在&#39;HTMLCanvasElement&#39;上执行&#39;toDataURL&#39; - OpenLayers: 'Failed to execute 'toDataURL' on 'HTMLCanvasElement'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM