node.js - 无法向客户端发送视频下载、错误、进度的响应
问题描述
如何使用正在进行的视频下载状态更新客户端。发生错误,完成我在不同事件上获得控制台,但无法发送响应。我正在使用管道发送视频作为响应,不想保存在后端
router.get("/", async (req, res) => {
res.header("Content-Disposition", 'attachment; filename="youtube.mp4');
const { url, ab_channel, quality } = req.query;
let completeUrl;
var received_bytes = 0;
var total_bytes = 0;
const video = await ytdl(videoID, { quality: 137 });
video.on("response", function (data) {
// Change the total bytes value to get progress later.
total_bytes = parseInt(data.headers["content-length"]);
});
video.on("data", function (chunk) {
// Update the received bytes
//send to response here
received_bytes += chunk.length;
showProgress(received_bytes, total_bytes);
/* res.write(received_bytes); */
});
video.on("error", function error(err) {
//send to response here
console.log("error 2:", err);
});
video.on("info", function (info) {
//send to response here
});
video.on("progress", function (info) {
//send to response here
console.log("Download progress");
});
video.on("end", function (info) {
console.log("Download finish");
/* res.send("File succesfully downloaded"); */
/* res.writeHead(200, { "Content-Type": "text/plain" });
res.end("ERROR File does not exist"); */
/* res.status(200).send(); */
});
// res.writeHead(200, {
// "Content-Type": "application/octet-stream",
// "Content-Disposition": "attachment; filename=" + fileName
// });
video.pipe(res);
});
解决方案
推荐阅读
- javascript - CSS:根据屏幕大小重新定位列
- android - emitAll 直到另一个操作完成?
- swift - 防止 TextEditor 在 ScrollView 内滚动
- javascript - 为什么其中一个对象有原型,而第二个对象没有?
- javascript - 如何在 JavaScript 中使用带有 sweetalert 的确认功能
- javascript - RxJs 扩展方法导致额外的 HTTP 调用
- javascript - 如何用 jest 测试 axios 拦截器
- node.js - graphql 订阅和 Socket io/Client 订阅通道的替代方案
- python - Pandas 填写最近日期
- c++ - Range-v3 中是否有办法将元素添加到范围/视图中?