这是一个流星应用程序。我需要生成一个 docx 文件并下载它。我正在通过运行来测试它:localhost:3000/download。
生成Word文件,但它完全是空的。
为什么?我将不胜感激任何建议!
这是我的服务器端代码:
const officegen = require('officegen');
const fs = require('fs');
Meteor.startup(() => {
WebApp.connectHandlers.use('/download', function(req, res, next) {
const filename = 'test.docx';
let docx = officegen('docx')
// Create a new paragraph:
let pObj = docx.createP()
pObj.addText('Simple')
pObj.addText(' with color', { color: '000088' })
pObj.addText(' and back color.', { color: '00ffff', back: '000088' })
pObj = docx.createP()
pObj.addText(' you can do ')
pObj.addText('more cool ', { highlight: true }) // Highlight!
pObj.addText('stuff!', { highlight: 'darkGreen' }) // Different highlight color.
docx.putPageBreak()
pObj = docx.createP()
let out = fs.createWriteStream(filename);
res.writeHead(200, {
'Content-Disposition': `attachment;filename=${filename}`,
'Content-Type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});
res.end(docx.generate(out));
});
});