使用node.js处理PLUpload上载 [英] Processing PLUpload uploads with node.js

查看:130
本文介绍了使用node.js处理PLUpload上载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在node.js中创建服务器端上传组件,但是我无法解释从PLUpload发送的信息。据我所知,PLUpload(在HTML5模式下)将文件作为二进制信息发送,这为我迄今为止尝试使用的node.js包创建了问题(node-formidable和node-express),因为他们期望正常带有多部分内容类型的HTML上传。

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 );


推荐答案

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

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
      ......

这篇关于使用node.js处理PLUpload上载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆