【问题标题】:Handling form data in nodejs在 nodejs 中处理表单数据
【发布时间】:2023-03-03 23:00:01
【问题描述】:

我正在从我的客户端 javascript 将一个 csv 文件作为 Post 请求上传到我的节点服务器。我也能够处理 nodejs 服务器上的请求。请帮助我获取文件并在服务器端解析文件。该文件将是一个 csv 文件,我需要解析该文件并读取该文件的内容。

我附上客户端上传文件的源代码sn-p以及下面的服务器端供参考。

myAngularApp.service('fileUpload', ['$http', function ($http) {
  this.uploadFileToUrl = function(file, uploadUrl){
    var fd = new FormData();
    fd.append('file', file);
    $http.post(uploadUrl, fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
    })
        .success(function(){
           // handling on success data
        })
        .error(function(){
           // handling on error data
        });
  }

在 NodeJs 服务器上:

router.post('/filter-reports', function(req, res) {
  console.log('Came inside the Node js router.. Now.. its all up to me to format the data....');
  console.log(req);
});

【问题讨论】:

  • Node/Express file upload 的可能重复项
  • 你提供的信息很少。所以你能在 req.body 和 req.files 上做一个 console.log 看看文件在哪里吗。我问这个是因为 express.4.0+ 我们不能使用 bodyparser 处理多格式数据。

标签: javascript node.js node.js-connect node.js-domains


【解决方案1】:

我认为您没有使用 multer 来处理 multipart/formdata 请求。

提及

var multer  =   require('multer');
var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './public');
  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '-' + Date.now());
  }
});

在上面的代码中。

/public 是保存文件的目录;您可以更改它,文件名将更改为带有时间戳的原始文件名。它们是此处的可选字段。

然后,在req.filereq.files 你会得到你的文件信息或访问文件req.files.path 将被使用,或者如果不想保存文件并想做操作,可以试试busboy在缓冲区中。

【讨论】:

    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 2014-08-05
    • 2015-12-28
    • 2012-11-30
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多