0

我有一个前端网络服务器 Node + Express,它从浏览器接收上传的文件。然后我想获取文件数据并通过 amqp 将其发送给工作人员。这是我接受上传并通过 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);
                    }
                });
            }
        });
    });
});
})
4

0 回答 0