处理PLUpload上传与node.js

我正在尝试在node.js中创build服务器端上传组件,但是我无法解释从PLUpload发送的信息。 从我所知道的来说,PLUpload(以HTML5模式)发送文件作为二进制信息,这为node.js包创build了问题,我一直试图使用到目前为止(node-formidable和node-express),因为他们期望正常HTML上传与多部分内容types。

对于它的价值,这是我一直在尝试使用的代码…

var formidable = require('formidable'); var sys = require('sys'); http.createServer( function( req, res ){ console.log('request detected'); if( req.url == '/upload/' ){ console.log('request processing'); var form = new formidable.IncomingForm(); form.parse( req, function( err, fields, files ){ res.writeHead( 200, { 'Access-Control-Allow-Origin': 'http://tksync.com', 'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE', 'Access-Control-Allow-Headers': '*', 'content-type': 'text/plain' }); res.write('received upload:\n'); res.end(sys.inspect({ fields: fields, files: files })); }); } }).listen( 8080 ); 

我没有问题使用plupload(在HTML5模式)与node.js与下面的代码:

 module.exports.savePhoto= (req, res) -> if req.url is "/upload" and req.method.toLowerCase() is "post" console.log 'savePhoto: req.url=', req.url, 'req.method=', req.method form = new formidable.IncomingForm() files = [] fields = [] form.uploadDir = config.PATH_upload form.on("field", (field, value) -> console.log field, value fields.push [ field, value ] ).on("file", (field, file) -> console.log field, file files.push [ field, file ] ).on "end", -> console.log "-> upload done: fields=", fields console.log "received fields:", util.inspect(fields) console.log "received files:", util.inspect(files) size = files[0][1].size pathList = files[0][1].path.split("/") #console.log 'pathList=', pathList file = pathList[pathList.length - 1] console.log "file=" + file ...... 

我创build了node-pluploader来处理这个问题,因为我发现elife的回答对于分块上传不起作用,因为所说的块有不同的请求。

基于快速的使用示例:

 var Pluploader = require('node-pluploader'); var pluploader = new Pluploader({ uploadLimit: 16 }); /* * Emitted when an entire file has been uploaded. * * @param file {Object} An object containing the uploaded file's name, type, buffered data & size * @param req {Request} The request that carried in the final chunk */ pluploader.on('fileUploaded', function(file, req) { console.log(file); }); /* * Emitted when an error occurs * * @param error {Error} The error */ pluploader.on('error', function(error) { throw error; }); // This example assumes you're using Express app.post('/upload', function(req, res){ pluploader.handleRequest(req, res); });