2015-01-05 43 views
0

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

我附上了源代码片段,用于在客户端和服务器端上传文件以供参考。

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); 
}); 
+0

[Node/Express文件上传]的可能重复(http://stackoverflow.com/questions/23691194/node-express-file-upload) – callmekatootie

+0

你给了一些信息。所以你可以做一个console.log关于req.body和req.files,看看这个文件在哪里。我问这个问题是因为使用express.4.0 +我们不能使用bodyparser来处理多形式的数据。 – harryy000

回答

-1

我想你不使用multer用于处理多/ 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将被使用,或给一个尝试打杂如果不想保存文件,并希望做缓冲操作。