2017-02-15 11 views
0

officegen npmモジュールを使用して、単語(docx)ファイルを作成してダウンロードします。過去に私はtempfileモジュールを使用して目的をダウンロードするための一時的なパスを作成しました。ここで私はWord文書のダウンロードのために書かれているコードは次のとおりです。Nodegenを使用してwordgenを作成してダウンロードする

var tempfile = require('tempfile'); 
var officegen = require('officegen'); 
var docx = officegen('docx'); 

router.post('/docx', function(req, res){ 
    var tempFilePath = tempfile('.docx'); 
    docx.setDocSubject ('testDoc Subject'); 
    docx.setDocKeywords ('keywords'); 
    docx.setDescription ('test description'); 

    var pObj = docx.createP({align: 'center'}); 
    pObj.addText('Policy Data', {bold: true, underline: true}); 

    docx.on('finalize', function(written) { 
     console.log('Finish to create Word file.\nTotal bytes created: ' + written + '\n'); 
    }); 
    docx.on('error', function(err) { 
     console.log(err); 
    }); 

    docx.generate(tempFilePath).then(function() { 
     res.sendFile(tempFilePath, function(err){ 
      if(err) { 
       console.log('---------- error downloading file: ' + err); 
      } 
     }); 
    }); 
}); 

それが作成した総バイト数も0私は間違って何をやっている示され

TypeError: dest.on is not a function

を言って私にエラーを与えますが?

+1

最新のofficegenを使用している場合、outはファイルだけでなくファイルストリームである必要があります。マニュアルhttps://github.com/Ziv-Barber/officegenを参照してください。また、 'docx.generate(res);' – Andrey

+0

@Andreyありがとうのような応答に直接生成することもできます。私はdoc.generate(res)をやってくれました。あなたの助けに感謝 – codeinprogress

答えて

2
router.post('/docx', function(req, res){ 
    var tempFilePath = tempfile('.docx'); 
    docx.setDocSubject ('testDoc Subject'); 
    docx.setDocKeywords ('keywords'); 
    docx.setDescription ('test description'); 

    var pObj = docx.createP({align: 'center'}); 
    pObj.addText('Policy Data', {bold: true, underline: true}); 

    docx.on('finalize', function(written) { 
     console.log('Finish to create Word file.\nTotal bytes created: ' + written + '\n'); 
    }); 
    docx.on('error', function(err) { 
     console.log(err); 
    }); 

    res.writeHead (200, { 
    "Content-Type": "application/vnd.openxmlformats-officedocument.documentml.document", 
    'Content-disposition': 'attachment; filename=testdoc.docx' 
    }); 
    docx.generate(res); 
}); 
関連する問題