简体   繁体   English

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

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

I am building an Ionic App and I am getting this error when I try to convert to base64 an image from an URL. 我正在构建一个Ionic应用程序,当我尝试从URL转换为base64图像时出现此错误。

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

My code is the following: 我的代码如下:

public getBase64Image(imgUrl: string): Promise<string> {
    if (!imgUrl.includes("http")) {
      return;
    }

    return new Promise<string>(resolve => {
      let img = new Image();

      img.src = imgUrl;
      img.crossOrigin = "anonymous"
      img.onload = (() => {
        let canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;
        let ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0);
        let dataURL = canvas.toDataURL("image/png");
        resolve(dataURL);
      });
    });
  }

I've read other questions where setting anonymous in crossOrigin was the solution, but I already have it. 我读过其他问题,在crossOrigin中设置anonymous是解决方案,但我已经有了。

Hope you can help me, thanks. 希望你能帮助我,谢谢。

EDIT 1 编辑1

As a note, I don't get this the first time that I convert an image to base64 but I get it in the following times I try to edit an image. 请注意,我不是第一次将图像转换为base64时得到此信息,但是在接下来的几次尝试编辑图像时得到了它。

Instead of the approach in the first post, I've switched to downloading the image using Angular's HttpClient and then getting its base64. 代替第一篇文章中的方法,我切换到使用Angular的HttpClient下载图像,然后获取其base64。

Code: 码:

public getBase64Image(imgUrl: string): Observable<string> {
    return new Observable(observer => {
      this.http.get(imgUrl, { responseType: 'blob' }).subscribe(data => {
        var reader = new FileReader();
        reader.readAsDataURL(data);
        reader.onloadend = () => {
          observer.next(reader.result.toString().replace(":application/octet-stream;", ":image/png;"));
          observer.complete();
        }
      });
    });
  }

暂无
暂无

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

相关问题 Angular 8 无法在“HTMLCanvasElement”上执行“toDataURL”:可能无法导出受污染的画布 - Angular 8 Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported 无法在“ HTMLCanvasElement”上执行“ toDataURL”:受污染的画布 - Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases 未捕获的DOMException:无法在'Window'上执行'postMessage' - Uncaught DOMException: Failed to execute 'postMessage' on 'Window' 未捕获的 DOMException:无法在“WorkerGlobalScope”上执行“importScripts” - Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope' 未捕获的 DOMException:无法在“WorkerGlobalScope”上执行“importScripts”:“http://localhost:9000/worker-html.js”处的脚本加载失败 - Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:9000/worker-html.js' failed to load Angular 6 - ERROR DOMException:无法在&#39;Element&#39;上执行&#39;setAttribute&#39;: - Angular 6 - ERROR DOMException: Failed to execute 'setAttribute' on 'Element': 错误 DOMException:无法在“文档”上执行“querySelector” - ERROR DOMException: Failed to execute 'querySelector' on 'Document' Angular 元素:DOMException:无法在“CustomElementRegistry”上执行“define” - Angular Element: DOMException: Failed to execute 'define' on 'CustomElementRegistry' 错误 DOMException:无法在“XMLHttpRequest”上执行“打开”:无效的 URL - ERROR DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL 错误 DOMException:无法在“元素”上执行“setAttribute”:“{{”不是有效的属性名称 - ERROR DOMException: Failed to execute 'setAttribute' on 'Element': '{{' is not a valid attribute name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM