代码之家  ›  专栏  ›  技术社区  ›  Steven Kanberg

base64从图形转换的照片显示为断开的图像

  •  3
  • Steven Kanberg  · 技术社区  · 6 年前

    我绞尽脑汁(和其他人)试图让这个工作。我拉一张照片通过MS图形API-这一部分工作良好。我能够接收数据(字节)。但是,我不能得到正确的图像转换为附加文件和张贴。

    Graph docs . 没有解决办法。npm包都会产生不同的输出,当我拍摄照片并上传到在线base64转换器时,没有一个输出与之匹配。另外,如果我进行在线转换并将输出字符串直接放入代码中,它就可以工作了。

    这是我代码的当前迭代。任何帮助都将不胜感激。

    var optionsPhoto = {
      url: "https://graph.microsoft.com/v1.0/me/photo/$value",
      method: "GET",
      headers: {
        Authorization: "Bearer " + token
      }
    };
    
    await request(optionsPhoto, function callback(error, response, body) {
      if (!error && response.statusCode == 200) {
        photoResponse.data = [
          {
            "@odata.type": "#microsoft.graph.fileAttachment",
            contentBytes: body.split(",").toString("base64"),
            contentLocation: "https://graph.microsoft.com/v1.0/me/photo/$value",
            isinline: true,
            Name: "mypic.jpg"
          }
        ];
        photoResponse.ContentType = response.headers["content-type"].toString();
        photoResponse.Base64string = (
          "data:" +
          photoResponse.ContentType +
          ";base64," +
          photoResponse.data[0].contentBytes
        ).toString();
      } else {
        console.log(error);
      }
    });
    

    .sendActivity 命令仅获取附件,如下所示:

    await dc.context.sendActivity({
      attachments: [
        { contentType: photoResponse.ContentType, contentUrl: photoResponse.Base64string }
      ]
    });
    
    1 回复  |  直到 6 年前
        1
  •  5
  •   Marc LaFleur    6 年前

    当你要照片的时候 /$value request 然而,客户将尸体视为 utf8 默认情况下基于字符串。

    为了重新训练原始二进制值,需要显式地告诉 请求 你不想发生这种事。这是通过设置 encoding: null

    encoding -要在上使用的编码 setEncoding null ,的 body 作为 Buffer . 还有别的吗? (包括 undefined 将作为 encoding 参数到 toString() (这意味着 默认情况下)。( 注: 如果需要二进制数据,应该设置 .)

    代码如下所示:

    var optionsPhoto = {
      url: "https://graph.microsoft.com/v1.0/me/photo/$value",
      encoding: null, // Tells request this is a binary response
      method: "GET",
      headers: {
        Authorization: "Bearer " + token
      }
    };
    
    await request(optionsPhoto, function callback(error, response, body) {
      if (!error && response.statusCode == 200) {
        // Grab the content-type header for the data URI
        const contentType = response.headers["content-type"];
    
        // Encode the raw body as a base64 string
        const base64Body = body.toString("base64");
    
        // Construct a Data URI for the image
        const base64DataUri = "data:" + contentType + ";base64," + base64Body;
    
        // Assign your values to the photoResponse object
        photoResponse.data = [
          {
            "@odata.type": "#microsoft.graph.fileAttachment",
            contentBytes: base64Body,
            contentLocation: optionsPhoto.url,
            isinline: true,
            Name: "mypic.jpg"
          }
        ];
        photoResponse.ContentType = contentType;
        photoResponse.Base64string = base64DataUri;
      } else {
        console.log(error);
      }
    });
    
    推荐文章