我有一个字符串作为const content="<div>How do I <b>convert </b> this string to file?</div>";,正确的方法是将其转换为html文件,并在最后将其转换为base64字符串。Buffer.from(content).toString('base64')仅将字符串转换为base64,而不转换为文件
发布于 2019-02-23 04:41:46
使用create-html
var fs = require('fs')
var createHTML = require('create-html')
var html = createHTML({
title: 'example',
body: '<div>How do I <b>convert </b> this string to file?</div>'
})
fs.writeFile('index.html', html, function (err) {
if (err) console.log(err)
})
console.log(fs.readFileSync('index.html').toString('base64'));有关更多信息,请参阅此链接https://www.npmjs.com/package/create-html
发布于 2019-02-23 04:23:21
Node file system API允许您轻松执行以下操作:
const fs = require('fs');
const content="<div>How do I <b>convert </b> this string to file?</div>";
// write `content` to `index.html`
fs.writeFileSync('index.html', content);
// read `index.html` in base64
console.log(fs.readFileSync('index.html').toString('base64'));https://stackoverflow.com/questions/54834563
复制相似问题