#如何用node做一個下載小片片的介面
以koa2為例
服務器端
async function downloadAvi (ctx, next) {
// 默認為json, 需要設定為二進制流哦
ctx.set('Content-type', 'application/octet-stream')
// 創建一個檔案讀取流
const reader = fs.createReadStream(path.resolve('service/番號.avi'))
// 發送
ctx.body = reader
}
客戶端
以vue專案為例
downloadAvi () {
axios.get(
'api/downloadAVI',
{responseType: 'blob'} /* axios默認json,一定要設定成二進制格式哦 */
).then(res => {
const data = res.data
const url = window.URL.createObjectURL(new Blob([data]))
const link = document.createElement('a')
link.href = url
link.download = '1.mp4'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/240030.html
標籤:其他
下一篇:Redis
