2014-12-04 74 views
0
我在解析使用流星

解析表單數據使用流星

這裏的multipart/form-data的問題

是我的路線:

this.route('mail', { 
    where: 'server', 
    path: '/mail', 
    action: function() { 
    console.log(this.request.body); 
    this.response.end('get request\n'); 
    } 
}); 

url編碼和JSON數據做工精細

回答

1

這是鐵路由器解析multipart/form-data中的一個錯誤: https://github.com/EventedMind/iron-router/issues/909

可以通過使用Busyboy模塊來解決:

var Busboy = Meteor.npmRequire("busboy") 
var fs  = Npm.require("fs"); 
var os  = Npm.require("os"); 
var path = Npm.require("path"); 

Router.onBeforeAction(function (req, res, next) { 
    var filenames = []; // Store filenames and then pass them to request. 

    if (req.method === "POST") { 
    var busboy = new Busboy({ headers: req.headers }); 

    busboy.on("file", function (fieldname, file, filename, encoding, mimetype) { 
     var saveTo = path.join(os.tmpDir(), filename); 
     file.pipe(fs.createWriteStream(saveTo)); 
     filenames.push(saveTo); 
    }); 

    busboy.on("field", function(fieldname, value) { 
     req.body[fieldname] = value; 
    }); 

    busboy.on("finish", function() { 
     // Pass filenames to request 
     req.filenames = filenames; 
     next(); 
    }); 

    // Pass request to busboy 
    req.pipe(busboy); 
    } else { 
    next(); 
    } 
});