2011-08-16 65 views
2

我試圖在node.js中創建一個服務器端上傳組件,但是我無法解釋從PLUpload發送的信息。據我所知,PLUpload(以HTML5模式)將文件作爲二進制信息發送,這爲我一直試圖使用的node.js包創建問題(node-formidable和node-express),因爲他們期望正常使用多部分內容類型上傳HTML。使用node.js處理PLUpload上傳

對於它的價值,這是我一直在嘗試使用代碼...

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

回答

2

我沒有問題,使用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 
     ...... 
1

我創建node-pluploader來處理這個問題,因爲我發現網上生活的答案並沒有爲分塊上傳工作,爲表示塊來在不同的請求。

基於快速使用率例如:

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