download csv to browser downloads node.js

170 Views Asked by At

I have this code:

    fs = require("fs");
  var downloadData = "select * from PRODUCTS"
    ibmdb.open(req.session.ibmdbconnDash, function (err, conn) {
        if (err) return console.log(err);
        conn.query(downloadData, function (err, rows) {
          if (err) {
            console.log(err);
          }


          const ws = fs.createWriteStream("productsDownloaded.csv");
          const jsonData = JSON.parse(JSON.stringify(rows));
          console.log("jsonData", jsonData);
          fastcsv
            .write(jsonData, { headers: true })
            .on("finish", function() {
              console.log("Write to productsDownloaded.csv successfully!");
            })
            .pipe(ws);


    
            var value = true;

            res.render("edit-products", {
              page_title: "edit-products",
              data: rows,
              userName: req.session.username,
              FN: req.session.firstname,
              LN: req.session.lastname,
              CO: req.session.company,
              value: value,
            });

          conn.close(function () {
            console.log("closing function p1 of delete product");
          });
        });
      });

however, it downloads but it doesn't go anywhere. So when im testing locally it goes into my vs code directory, but i want it to download by the browser, so do i have to send something to the front end? Like if i press the download button and trigger this function, it should download to the users directory. how can i achieve this?

0

There are 0 best solutions below