Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

node.js - res.download error Request Aborted on large zip file express, fast-csv, zip

I am using fast-csv to convert the data from mongodb to CSV format, and then save it to the products folder where all the banner images are also present. So I am zipping products folder with all the banner images and CSV file and return zip file with res.download as response on the API call.

The following code only contains the necessary part.

import * as csv from "fast-csv";
const ws = fs.createWriteStream("products/file.csv");

 csv
    .write(data, {
        headers: [
          "_id",
          "name",
          "description",
          "banner",
        ],
      })
      .on("finish", function () {
        // console.log("Write to csv successfully!");
      })
      .pipe(ws)
      .on("close", async function () {
        try {
          if (!fs.existsSync("tmp")) {
            fs.mkdirSync("tmp");
          }
          await zipDir("products", "tmp/zipfile.zip");
          res.download("tmp/zipfile.zip", "products.zip", async function (err) {
            if (err) {
              console.error(err);
            }
          });
        } catch (error) {
          console.error(error);
        }
      });

This code works when there are less products and zip file is small but fails for large zip file with error

Error: Request aborted
    at onaborted (/home/user/project/node_modules/express/lib/response.js:1025:15)
    at Immediate._onImmediate (/home/user/project/node_modules/express/lib/response.js:1067:9)
    at processImmediate (internal/timers.js:461:21) {
  code: 'ECONNABORTED'
question from:https://stackoverflow.com/questions/65898999/res-download-error-request-aborted-on-large-zip-file-express-fast-csv-zip

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...