我正在尝试在node.js中创建服务器端上传组件,但是我无法解释从PLUpload发送的信息。据我所知,PLUpload(在HTML5模式下)将文件作为二进制信息发送,这为我迄今为止尝试使用的node.js包创建了问题(node-formidable和node-express),因为他们期望正常带有多部分内容类型的HTML上传。
对于它的价值,这是我一直试图使用的代码......
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 );
答案 0 :(得分:2)
使用带有以下代码的node.js的plupload(在HTML5模式下)我没有问题:
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
......
答案 1 :(得分:1)
我创建node-pluploader
来处理这个问题,因为我发现elife的回答并不适用于分块上传,因为所说的块有不同的请求。
基于Express的用法示例:
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);
});