简体   繁体   English

使用 Node 归档多个文件并立即下载

[英]Archive multiple files with Node and download it immediately

I'm trying to zip multiple files on a server and download it when user requests it.我正在尝试 zip 服务器上的多个文件,并在用户请求时下载它。 I am using adm-zip to zip files.我正在对 zip 文件使用 adm-zip。

Files add perfectly fine and are zipped.文件添加得非常好并且被压缩。 zip.writeZip('') zipes files perfectly and saves them to local server. zip.writeZip('') 完美压缩文件并将它们保存到本地服务器。 I am just unable to download them.我只是无法下载它们。 Either way I think it would be better if I could send zip directly via buffer.无论哪种方式,我认为如果我可以通过缓冲区直接发送 zip 会更好。

router.get(
  "/download-zip/:season_id/:club_id",
  passport.authenticate("jwt", { session: false }),
  (req, res) => {
    FileSubmission.find({
      season: req.params.season_id,
      club: req.params.club_id
    }).then(files => {

      const zip = new AdmZip();

      files.forEach(file => {
        // add local file
        zip.addLocalFile(`uploads/club-uploads/${file.club}/${file.name}`);
      });

      res.download(zip.toBuffer(), "asd.zip");
    });
  }
);

On the front I am using react with actions and js-file-download library在前面,我正在使用对动作和 js-file-download 库做出反应


// Download ALL FILES by season ID and club ID
import fileDownload from "js-file-download";

export const downloadAllFiles = (season_id, club_id) => dispatch => {
  axios
    .get(`/api/files/download-zip/${season_id}/${club_id}`)
    .then(response => {
      fileDownload(response.data, `All Files- ${club_id}.zip`);
    })
    .catch(err => {
      console.log(err);
      dispatch({ type: GET_ERRORS, payload: err.response.data });
    });
};

Ok, don't know why I got downvoted, but here's the answer on how I managed to create temporary archive, download it and delete it.好的,不知道为什么我被否决了,但这是我如何设法创建临时存档、下载并删除它的答案。

Here's back-end这里是后端

// Create a ZIP of files uploaded by a club
router.get(
  "/download-zip/",
  (req, res) => {
      zip.addLocalFile(/*ADD THE FILES TO ZIP*/);

      zip.writeZip(
        `temp/zips/temp-file.zip`,
        err => {
          if (err) {
            console.log(err);
          }

          res.download(
            `uploads/club-zips/test-file.zip`,
            function(err) {
              if (!err) {
                //delete file after it's been downloaded
                fs.unlinkSync(
                  `uploads/club-zips/test-file-${req.params.club_id}.zip`
                );
              }
            }
          );
        }
      );
    });
  }
);

Here's what I use on front.这是我在前面使用的。 As I said I used package called js-file-download to download blobs正如我所说,我使用名为 js-file-download 的 package 来下载 blob

export const downloadAllFiles = () => dispatch => {
  axios
    .get(`/api/files/download-zip/`, {
      responseType: "arraybuffer"
    })
    .then(response => {
      fileDownload(response.data, `all-files.zip`);
    })
    .catch(err => {
      console.log(err);
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM