【发布时间】:2021-07-01 00:43:38
【问题描述】:
我不知道如何正确下载和保存 PNG 图片。
我将尝试从这个网站获取我的个人资料图片作为示例,使用 axios:
const fs = require("fs");
const axios = require("axios");
axios
.get("https://i.stack.imgur.com/VQs8o.png")
.then((res) => {
console.log(res.data); //returns gibberish V�&ZJ���%rl�D�*=Y�����
fs.writeFile("./profile.png", res.data, (err) => {
if (err) console.log(err);
});
})
.catch((err) => {
if (err) console.log(err);
});
当我将图像写入文件并尝试打开它时,它会说发生错误或它不是 PNG 文件。
当我记录收到的数据时,它显示为一堆乱码,带有如下字符:
'��BNږ��"V�&ZJ���%rl�D�*=Y�������?��w�p��}0���S�κ���u!p
我尝试将其保存到 node.js 中每个 encoding type 的文件中。我试过使用Buffer.from(),但没有任何效果。
我不知道我错过了什么。我的问题是:如何以正确的格式存储图像?为什么会出现一堆乱码,有没有办法解码成base64url?
【问题讨论】:
标签: node.js http image-processing web-scraping axios