javascript - How to download a file with nodeJS
问题描述
I want to download an image file with nodeJS, by using an API, but the problem is the API link doesn't have .jpg
file in the end, how do I do,
below is how I am trying
url = 'https://i.pravatar.cc/225'
const https = require('https')
const fs = require('fs');
result = https.get(url, (resp) => {
console.log('Result of response: ', resp)
fs.writeFileSync('apiResponse', resp)
console.log('Reached end!')
})
When I click the URL it shows the image in browser, how do make my program to write the file on hard-drive,
解决方案
只是管道响应文件
const url = 'https://i.pravatar.cc/225'
const https = require('https')
const fs = require('fs');
https.get(url, resp => resp.pipe(fs.createWriteStream('./test.jpeg')));
推荐阅读
- python - 根据一些验证计算分数,使用权重和所需的成功验证
- apache-dolphinscheduler - 具有依赖关系的任务节点之间的故障策略问题
- vue.js - bootstrap-vue 2 b-table 插槽停止工作
- c++ - 无法将“std::string&”类型的非常量左值引用绑定到“std::string”类型的右值
- java - 分析从 Oracle Java 迁移到 Azul Zulu Open SDK 的回归影响
- python - TensorFlow:logits 和标签必须具有相同的第一维,得到 logits 形状 [120,4] 和标签形状 [480]
- android - Google vision 条形码扫描仪不适用于 android 6.0 (Marshmallow)
- docker - 如何将两个 docker 图像合二为一?
- reactjs - TextField: helperText 跨越多行
- mysql - 需要以下要求的 SQL 查询