I'm attempting to create a server-side upload component in node.js, but I'm having trouble interpreting the information sent from PLUpload. From what I can tell, PLUpload (in HTML5 mode) sends files as binary information, which creates problems for the node.js packages I've been attempting to use so far (node-formidable and node-express), as they expect normal HTML uploads with multipart content types.
For what it's worth, this is the code I've been attempting to use...
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 );
I have no problem to use plupload(in HTML5 mode) with node.js with below code:
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
......
I created node-pluploader
to handle this, as I found elife's answer didn't work for chunked uploads, as said chunks come in on different requests.
Express-based usage example:
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);
});
来源:https://stackoverflow.com/questions/7081090/processing-plupload-uploads-with-node-js