问题描述: 在使用axios发送请求时,无法接收到流式响应。
解决方法:
responseType参数指定响应类型为stream。axios.get(url, { responseType: 'stream' })
.then(response => {
// 处理流式响应
response.data.pipe(fs.createWriteStream('file.jpg'));
})
.catch(error => {
console.error(error);
});
axios的transformResponse配置项,手动处理流式响应。axios.get(url, { transformResponse: [] })
.then(response => {
// 处理流式响应
response.data.pipe(fs.createWriteStream('file.jpg'));
})
.catch(error => {
console.error(error);
});
http模块发送请求,然后手动处理流式响应。const http = require('http');
http.get(url, response => {
response.pipe(fs.createWriteStream('file.jpg'));
});
以上是几种可能的解决方法,根据实际情况选择适合自己的方式。