nodejs 追加写入appendFile

追加写入appendFile

具有创建文件和追加写入的功能
fs.appendFile(路径,内容,回调函数)
fs.appendFileSync(路径,内容)

如果文件不存在会创建文件,如果文件存在会追加到末尾

fs.appendFile(’./data1.txt’,‘hello\n’,(err)=>{
if(err) throw err;
console.log(‘写入成功’);
});

例如:将数组写入文件中

var arr=[‘tom’,‘jarry’,‘scott’,‘king’];
for(var i=0;i<arr.length;i++){
a=arr[i];
fs.appendFileSync(’./data1.txt’,a);
fs.appendFileSync(’./data1.txt’,’\n’)
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将图片写入到 Word 中,可以使用 `docxtemplater` 模块和 `officegen` 模块来实现。以下是一个使用 Node.js 将图片写入 Word 文档的示例代码: ```javascript const fs = require('fs'); const Docxtemplater = require('docxtemplater'); const ImageModule = require('docxtemplater-image-module'); const officegen = require('officegen'); // 读取 Word 模板文件 const content = fs.readFileSync('template.docx', 'binary'); // 创建 Docxtemplater 实例 const doc = new Docxtemplater(); // 加载图片模块 const imageModule = new ImageModule({ centered: false, getImage: (tagValue, tagName) => { // 读取图片文件 const img = fs.readFileSync(tagValue); // 创建 officegen 模块的 image 对象 const image = officegen('png'); image.setData(img); return image; }, getSize: (img, tagValue, tagName) => { return [200, 200]; }, getImageName: (tagValue, tagName) => { return 'My image'; }, }); // 绑定图片模块到 Docxtemplater 实例 doc.attachModule(imageModule); // 编译模板 doc.loadZip(content); doc.compile(); // 渲染数据 const data = { image: 'image.png', }; const result = doc.render(data); // 将渲染结果写入 Word 文件 fs.writeFileSync('output.docx', result); ``` 在上述代码中,我们首先使用 `fs` 模块读取 Word 模板文件,然后创建一个 `Docxtemplater` 实例,并使用 `ImageModule` 模块来加载图片。接着,我们将图片模块绑定到 `Docxtemplater` 实例中,并编译模板。最后,我们使用 `render` 方法渲染数据,并将渲染结果写入 Word 文件中。 需要注意的是,上述代码中使用的图片格式为 PNG 格式,如果需要使用其他格式的图片,需要相应地修改代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值