0
我想使用officegen npm模塊創建一個單詞(docx)文件並下載它。在過去,我使用tempfile模塊來創建下載目的的臨時路徑。這裏是代碼我寫的word文檔下載:使用officegen創建和下載Word文檔的Node.js
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);
}
});
});
});
但是它給了我一個錯誤說
TypeError: dest.on is not a function
創建也顯示爲0。我在做什麼錯的總字節?
如果您使用的是最新的officegen,那麼out應該是文件流,而不僅僅是文件。請參閱手冊https://github.com/Ziv-Barber/officegen。你也可以直接生成'docx.generate(res);' – Andrey
@Andrey謝謝。我做了doc.generate(res),它工作。感謝你的幫助 – codeinprogress