首页 > 解决方案 > 无法向客户端发送视频下载、错误、进度的响应

问题描述

如何使用正在进行的视频下载状态更新客户端。发生错误,完成我在不同事件上获得控制台,但无法发送响应。我正在使用管道发送视频作为响应,不想保存在后端

 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);
        });

标签: node.js

解决方案


推荐阅读