在 Nodejs 中使用 Unirest 将图像保存到文件
Save Image to File with Unirest in Nodejs
我正在尝试使用 Unirest 下载图片,但保存后图片已损坏
const downloadChapterPage = async (url, pageName) => {
return new Promise(async (resolve) => {
unirest.get(url)
.end(async (res) => {
if (res.error) {
console.log("Error when downloading page : " + pageName, " ", res.error)
resolve();
}
fs.writeFileSync(pageName + '.jpg', res.raw_body);
resolve();
});
});
}
downloadChapterPage('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'page1');
我也用这种方式起诉过,但也行不通
const downloadChapterPage = async (url, pageName) => {
return new Promise(async (resolve) => {
unirest.get(url)
.end(async (res) => {
if (res.error) {
console.log("Error when downloading page : " + pageName, " ", res.error)
resolve();
}
const data = Buffer.from(res.raw_body);
fs.writeFileSync(pageName + '.jpg', data, 'binary');
resolve();
});
});
}
downloadChapterPage('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'page1');
这个修改怎么样?
修改点:
- 我认为编码可能是您遇到问题的原因。所以请在请求中添加
encoding(null)
。
- 这张图片
https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
的 mimeType 是 image/png
。
修改后的脚本:
const downloadChapterPage = async (url, pageName) => {
return new Promise(async (resolve) => {
unirest.get(url)
.encoding(null) // Added
.end(async (res) => {
if (res.error) {
console.log("Error when downloading page : " + pageName, " ", res.error)
resolve();
}
const data = Buffer.from(res.raw_body);
fs.writeFileSync(pageName + '.png', data, 'binary'); // Modified or fs.writeFileSync(pageName + '.png', data);
resolve();
});
});
}
参考:
我正在尝试使用 Unirest 下载图片,但保存后图片已损坏
const downloadChapterPage = async (url, pageName) => {
return new Promise(async (resolve) => {
unirest.get(url)
.end(async (res) => {
if (res.error) {
console.log("Error when downloading page : " + pageName, " ", res.error)
resolve();
}
fs.writeFileSync(pageName + '.jpg', res.raw_body);
resolve();
});
});
}
downloadChapterPage('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'page1');
我也用这种方式起诉过,但也行不通
const downloadChapterPage = async (url, pageName) => {
return new Promise(async (resolve) => {
unirest.get(url)
.end(async (res) => {
if (res.error) {
console.log("Error when downloading page : " + pageName, " ", res.error)
resolve();
}
const data = Buffer.from(res.raw_body);
fs.writeFileSync(pageName + '.jpg', data, 'binary');
resolve();
});
});
}
downloadChapterPage('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'page1');
这个修改怎么样?
修改点:
- 我认为编码可能是您遇到问题的原因。所以请在请求中添加
encoding(null)
。 - 这张图片
https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
的 mimeType 是image/png
。
修改后的脚本:
const downloadChapterPage = async (url, pageName) => {
return new Promise(async (resolve) => {
unirest.get(url)
.encoding(null) // Added
.end(async (res) => {
if (res.error) {
console.log("Error when downloading page : " + pageName, " ", res.error)
resolve();
}
const data = Buffer.from(res.raw_body);
fs.writeFileSync(pageName + '.png', data, 'binary'); // Modified or fs.writeFileSync(pageName + '.png', data);
resolve();
});
});
}