【发布时间】:2020-09-09 19:14:03
【问题描述】:
我需要使用 Axios 从流中下载文件。我不认为这是服务器的问题,因为如果我使用 http 包,它就可以工作。这是我的代码:
export function downloadRequest (savePath, reqURL, currentFile, serverPath) {
const file = fs.createWriteStream(savePath[0] + `/${currentFile}`)
axios({
method: 'get',
url: `${storedIP}${reqURL}`,
responseType: 'stream',
headers: {
Authorization: `Bearer ${token}`
}
}).then(function (reponse) {
reponse.data.pipe(file)
hashCheck(currentFile, savePath, serverPath)
})
但是这会返回以下两个错误:
the provided value 'stream' is not a valid enum value of type XMLHttpRequestResponseType.
Uncaught (in promise) TypeError: reponse.data.pipe is not a function
我试着做这篇文章中所说的:Type error this.httpClient.get(...).pipe is not a function
但这使得一个窗口弹出说这样的话:
windows script host
Script: c:\npm\node_modules\webpack\bin\webpack.js
Line: 1
Char: 1
Error: Invalid character
Code: 800A03F6
Source: Microsoft JScript compilation error
我进入了那个 js 文件,第 1 行是#!/usr/bin/env node
我也尝试卸载 webpack 并重新安装,但没有帮助。
注意:如果这很重要,我正在使用带有类星体的 Axios。
【问题讨论】:
-
“我需要使用 Axios 从流中下载文件”——为什么?错误消息表明 Axios 不支持流,并且您说您有一个使用 http 模块的有效解决方案。
-
这是在服务器上,还是你使用
electron或类似的东西? -
@Quentin 好吧,我想我不需要,我只是喜欢 Axios 的一些功能,例如:
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;和:onDownloadProgress allows handling of progress events for downloadsalos 在文档中说:responseType indicates the type of data that the server will respond with options are: 'arraybuffer', 'document', 'json', 'text', 'stream'跨度> -
@MarcosCasagrande 我正在使用电子和类星体,但这不在服务器上。服务器只是普通的nodejs,但正在另一台电脑上运行
-
检查我的答案。
标签: javascript node.js