【问题标题】:How to convert the string type from an API response to an image file - ����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001 -如何将字符串类型从 API 响应转换为图像文件 - ����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001 -
【发布时间】:2018-05-28 03:04:20
【问题描述】:

我已经使用 https://graph.microsoft.com/beta/me/photo/$value API 来获取 Outlook 用户的个人资料图片。我得到一张在 rest-client 中运行上述 API 的图像。 API 的 content-type 为“image/jpg”

但是,在 Node.js 中,API 的响应如下:

����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000�\u0000\u0005\u0005\u0005\u0005\u0005\u0005\u0006\u0006\u0006\u0006\b\t\b\t\b\f\u000b\n\n\u000b\f\u0012\r\u000e\r\u000e\r\u0012\u001b\u0011\u0014\u0011\u0011\u0014\u0011\u001b\u0018\u001d\u0018\u0016\u0018\u001d\u0018+"\u001e\u001e"+2*(*2<66<LHLdd�\u

我使用“fs”来创建图像文件。代码如下:

const options = {  
    url: "https://graph.microsoft.com/beta/me/photo/$value",
    method: 'GET',
    headers: {
        'Accept': 'application/json',
        'Authorization': `Bearer ${locals.access_token}`,
        'Content-type': 'image/jpg',
    }
};

request(options, (err, res, body) => {  
    if(err){
        reject(err);
    }
    console.log(res);
    const fs = require('fs');
    const data = new Buffer(body).toString("base64");
    // const data = new Buffer(body);
    fs.writeFileSync('profile.jpg', data, (err) => {
        if (err) {
            console.log("There was an error writing the image")
        }
        else {
            console.log("The file is written successfully");
        }
    });
});

文件写入成功,但生成的.jpg图像文件损坏。我无法打开图像。 图像文件的输出如下:

77+977+977+977+9ABBKRklGAAEBAAABAAEAAO+/ve

【问题讨论】:

  • 上面写着image/jpg。所以试着写.jpg而不是.png
  • 感谢@vibhor1997a> 我已将问题更新为.jpg。我也得到了.jpg 的相同回复。
  • @DineshKumar 您可以将图像保存到数据库并获取 url?或保存 base64。
  • 谢谢@zabusa。我无法在本地打开图像。我认为在数据库中存储损坏的文件没有意义。
  • .jpg 的正确 MIME 类型是 image/jpeg,有人搞砸了。

标签: javascript node.js microsoft-graph-api fs


【解决方案1】:

请求是deprecated。你可以用 axios 做到这一点;

// GET request for remote image in node.js
axios({
  method: 'get',
  url: 'http://example.com/file.jpg',
  responseType: 'stream'
})
  .then(function (response) {
    response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
  });

【讨论】:

    【解决方案2】:

    这样做的原因是默认情况下,request 会在响应数据上调用.toString()。对于二进制数据,例如 RAW JPEG,这不是您想要的。

    request 文档中也有解释(虽然含糊不清):

    (注意:如果你期望二进制数据,你应该设置encoding: null。)

    这意味着你也可以使用它:

    const options = {  
      encoding : null,
      url      : "https://graph.microsoft.com/beta/me/photo/$value",
      method   : 'GET',
      headers  : {
        'Accept'        : 'application/json',
        'Authorization' : `Bearer ${locals.access_token}`,
        'Content-type'  : 'image/jpg',
      }
    };
    

    但是,流式传输可能仍然是更好的解决方案,因为它不需要先将整个响应读入内存。

    【讨论】:

      【解决方案3】:

      您可以通过像这样流式传输响应来做到这一点,

      request(options,(err,res,body)=>{
        console.log('Done!');
      }).pipe(fs.createWriteStream('./profile.jpg'));
      

      https://www.npmjs.com/package/request#streaming

      https://nodejs.org/api/fs.html#fs_class_fs_writestream

      【讨论】:

      • 感谢@vibhor1997a。这就说得通了。干杯。
      猜你喜欢
      • 2020-07-06
      • 2022-01-03
      • 2015-05-13
      • 2010-09-24
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      相关资源
      最近更新 更多