Nodejs amqp缓冲区传输

时间:2012-08-18 10:13:52

标签: node.js buffer amqp

我有一个前端网络服务器Node + Express,它从浏览器接收上传的文件。然后我想获取文件数据并通过amqp将其发送给worker。这是我接受上传并通过amqp发送文件的代码:

    adminCommercialRegistration : function (req,res,next) {
    if(!this.checkLogin (req,res)) return;

        fs.readFile(req.files.html.path, function (err,data) {
            if(err)
            {
                res.json('Error reading uploaded file');
            }
            else
            {
                var metaData = {
                                companyName : req.body.companyName,
                                date : req.body.date
                               };
                var payLoad = { metaData : metaData, data : data };
                var connection = require('amqp').createConnection({ host : '172.27.164.28'});
                    connection.on('ready', function(){

                        console.log('Connected to ' + connection.serverProperties.product);

                        var exchange = connection.exchange('ooparoopa');
                        var queue = connection.queue('htmlFileUpload');

                        queue.on('queueDeclareOk', function(args) {
                            console.log('Queue Opened');
                            queue.bind(exchange, '#html');

                            queue.on('queueBindOk',function (){
                            console.log('Queue Bound');

                            exchange.publish('#html', { payLoad : payLoad });
                            res.redirect('/commercial');
                        });
                    });
                });                 
            }
        });
    },

这部分工作正常并将发送数据。我已经检查了这一侧的数据对象,它是一个缓冲区。

以下代码是工作进程。它接收数据并将所需信息添加到数据库。但是,文件数据存在问题。它看起来不像是一个缓冲区。我得到错误消息未知编码。我该怎么做才能让它回到二进制缓冲区?

connection.on('ready', function(){
console.log('Connected to ' + connection.serverProperties.product);

var exchange = connection.exchange('ooparoopa');
var queue = connection.queue('htmlFileUpload');

queue.on('queueDeclareOk', function(args){
    queue.bind(exchange, '#html');

    queue.subscribe({ack:true}, function (payLoad) {
        queue.shift();
        console.log('Message Received:');
        console.log(payLoad.payLoad.metaData.companyName);
        var Commercial = db.model('Commercial');
        var newCommercial = new Commercial({ companyName : payLoad.payLoad.metaData.companyName, date : payLoad.payLoad.metaData.date });
        newCommercial.save(function (err,newComm){
            if(err)
            {
                console.log(err);
            }
            else
            {

                console.log(payLoad.payLoad.data.length)
                var folder = '/var/www/ooparoopastatic/html/';
                var _id = newComm.id;
                var filename = _id + '.zip';

                console.log(payLoad.payLoad.data);
                fs.writeFileSync(folder + filename, buffer, function (err) {
                    if(err)
                    {
                        console.log(err);
                    }
                    else
                    {
                        console.log('Writing to file at:' + folder + filename);
                    }
                });
            }
        });
    });
});
})

0 个答案:

没有答案