【发布时间】:2021-07-07 23:04:04
【问题描述】:
我有以下视频
网址:https://static.videezy.com/system/resources/previews/000/000/161/original/Volume2.mp4
并想用Axios逐块下载它并写入响应(发送到客户端)
这里不知道怎么用Range Header
const express = require('express')
const app = express()
const Axios = require('axios')
app.get('/download', (req, res) => {
downloadFile(res)
})
async function downloadFile(res) {
const url = 'https://static.videezy.com/system/resources/previews/000/000/161/original/Volume2.mp4'
console.log('Connecting …')
const { data, headers } = await Axios({
url,
method: 'GET',
responseType: 'stream'
})
const totalLength = headers['content-length']
let offset = 0
res.set({
"Content-Disposition": 'attachment; filename="filename.mp4"',
"Content-Type": "application/octet-stream",
"Content-Length": totalLength,
// "Range": `bytes=${offset}` // my problem is here ....
});
data.on('data', (chunk) => {
res.write(chunk)
})
data.on('close', function () {
res.end('success')
})
data.on('error', function () {
res.send('something went wrong ....')
})
}
const PORT = process.env.PORT || 4000
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`)
})
【问题讨论】:
标签: javascript node.js axios chunking