【问题标题】:Processing PLUpload uploads with node.js使用 node.js 处理 PLUpload 上传
【发布时间】:2011-10-28 04:59:27
【问题描述】:

我正在尝试在 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 );

【问题讨论】:

    标签: javascript http node.js upload plupload


    【解决方案1】:

    通过以下代码将 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
          ......
    

    【讨论】:

      【解决方案2】:

      我创建了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);
      });
      

      【讨论】: