【问题标题】:save binary to an image file in node js将二进制文件保存到节点js中的图像文件
【发布时间】:2021-02-27 08:25:56
【问题描述】:

我正在以二进制形式接收 response.data 在记事本中打开时,我可以保存一个文件 test.jpg,它是这样的二进制文本:

���� JFIF H H ��*Exif MM * b j( 1 r2 ��i � � �� ' �� 'Adobe Photoshop CS4 Windows 2009:01:31 22:25:45 ��� � @� > &( . � HH ���� JFIF HH �� Adob​​e_CM ��!1AQa"q�2���B# $R�b34r��C%�S���cs5���&D�TdE£t6�U�e���u��F'����������������Vfv ��������7GWgw�������� 5 !1AQaq"2����B#�R��3$b�r��CScs4�%��&5��D�T �dEU6te����u��F����������A�̰Y�u��7��l8��Z��~e����eXj�Γ����e ��fSˁ�'E���q��X%h�����������2v�8#������llx;����h $���

var img = response.data;
var path = "D:/Picture";
var filename = "test.jpg";
var buf = Buffer.from(img, 'binary');

fs.writeFileSync(path + "/" + filename, buf, function(error) {
 if (error) {
  console.error("write error:  " + error.message);
  } else {
  console.log("Successful Write to " + path);
 }
});

无法作为图片打开 我怎么了?

【问题讨论】:

  • 为什么你需要二进制,你能不能只使用简单的base64图像编码?
  • 实际上,来自 api api-data.line.me/v2/bot/message{messageId}/content 行的 response.data 二进制文件我该怎么办?

标签: node.js buffer fs


【解决方案1】:

你可以试试这个,

fs.writeFile("test.jpg", response.data, 'binary', function (err) {
    if (err) throw err;
    console.log('File saved.')
});
  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 2018-06-25
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多