首页 > 解决方案 > 从 Angular 应用程序中的 html 对象获取图像数据并将其发送到休息服务器

问题描述

我想做的是:

从我的 Angular(在 Typescript!没有 javascript!)应用程序中的 html 对象中获取图像数据并将其发送到我的休息服务器。这应该由我的 service with function 完成set_Image

到目前为止一切正常,我得到了正确的数据,用 console.log 函数显示。在其他服务功能中也能http.post(url,Image64).pipe(tap(Image64 => console.log(Image64)));很好地工作。

因为我在函数体中声明了必要的变量,所以我无法访问我的变量function (canvas) {}但是现在我得到了错误:

"ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'post' of undefined."

我现在在这个问题上工作了两个星期,我所有的研究都无法解决这个问题。请帮忙。

编码:

import { HttpClient, HttpHeaders }            from '@angular/common/http';
import * as html2canvas from 'html2canvas'

const url1 = 'http://localhost:55320/api/Server/';
.
.
.
  set_Image(container:string)
  {      
    html2canvas(document.getElementById(container))
    .then(function (canvas) 
    {
      var Image64 = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
      console.log("CANVAS: ", Image64);
      const url = url1 + '0/set_Image';
      var http:HttpClient;
      http.post(url,Image64).pipe(tap(Image64 => console.log(Image64)));
    });
  }

标签: angularhtml2canvas

解决方案


好像在构造函数中,您是否为 HttpClient 服务创建了实例。

构造函数(私有http:HttpClient)

您可以使用 Http 相关的公共 API 访问。

这个.http.post()。


推荐阅读