Angularjs 使用NodeJS下载文件

Angularjs 使用NodeJS下载文件,angularjs,node.js,Angularjs,Node.js,我正在用node js创建一个csv文件,现在想将其下载到用户浏览器的默认下载位置。我们将seneca与nodejs一起使用,csv文件将保存在服务器上。现在,当使用将单击前端的导出(基于角度),节点将创建一个csv并将其下载到用户机器。我们怎样才能做到这一点呢?我认为你不能。。。Nodejs是服务器端。。。所以你放在Nodejs下的任何东西都会进入服务器。。。 如果用户单击某个按钮或某个其他事件触发生成CSV文件的进程,然后用户必须选择保存位置,否则文件将自动下载到用户浏览器设置下指定的默认下

我正在用node js创建一个csv文件,现在想将其下载到用户浏览器的默认下载位置。我们将seneca与nodejs一起使用,csv文件将保存在服务器上。现在,当使用将单击前端的导出(基于角度),节点将创建一个csv并将其下载到用户机器。我们怎样才能做到这一点呢?

我认为你不能。。。Nodejs是服务器端。。。所以你放在Nodejs下的任何东西都会进入服务器。。。
如果用户单击某个按钮或某个其他事件触发生成CSV文件的进程,然后用户必须选择保存位置,否则文件将自动下载到用户浏览器设置下指定的默认下载目录…

可以将使用Node.js生成的动态文件下载到浏览器的默认下载位置。有许多帖子概述了如何使用Express helper res.download()从服务器检索静态文件。针对你的问题,有一种方法可以实现你的要求

在解释您的问题时,遵循以下过程:

  • 当用户单击导出时,用户生成的数据将发送到服务器进行处理
  • 节点处理数据并生成要下载的文件,无需第二次用户交互(用户单击“导出”并下载该文件)

    • 客户

      //Export button
      $("#exportBtn").click(function () {
      //Code to generate data for the CSV and save it to the src variable
      var src = csvData;
      
          //Send the CSV data to Node for processing and file generation.
          $.post("http://localhost:3000/submitcsv", { csv: src }, function (data) {
              //Check the returned file name in data - this check depends on your needs; ie: regex
              if (data) {
                  //request the file that is to be downloaded to the client. Optionally use $window.open()
                  window.location.href = "http://localhost:3000/app/" + data;
              }
          });
      });
      
    • 服务器

      //Post data from the client
      app.post('/submitcsv', function (req, res) {
          var async = require('async');
      
          var fs = require('fs');
          var path = require('path');
      
          var csvData = req.body.csv;
      
          function processCSV(callback) {
              //Code to create the csv file and a uniqueIdentifier
              callback();
          }
      
          function finalize() {
              //Save the CSV to the server. This is a specific location on the server in /app. 
              //You can use Express.static paths that suit your setup.
              fs.writeFile('./app/temp/csvFile' + uniqueIdentifier + '.csv', buf, function (err) {
                  if (err) {
                      console.log(err);
                      res.send(500, "Something went wrong...");
                  }
                  else {
                      console.log("CSV file is saved");
                      //Send the file name and location back to the client
                      res.send('/temp/csvFile' + uniqueIdentifier + '.csv');
                  }
              });
          }
          // After the CSV data is processed, save the file and send it to the client.
          async.series([
              processCSV
          ], finalize);
      }); 
      
      //Send the requested file back to the client
      app.get('./app/:csvFile', function (req, res){
          var c = req.params.csvFile;
          res.download(c);
          //Code to delete the file if it is temporary via fs.unlink
      });
      
  • 虽然为了简单起见,上面的代码中没有显示,但建议以某种方式保护发送和接收此类数据的/app路由。对受保护的路由使用Passport策略将类似于:

    app.all('/app/*', function(req, res, next) {
      //Check that the user has previously been authenticated
      if (req.user){
          next(); 
      } else {
        // require the user to log in
        res.redirect("/login"); 
      }
    });