简体   繁体   中英

Convert string to file and then to base64

I have a string as const content="<div>How do I <b>convert </b> this string to file?</div>"; What's the correct way to convert this to a html file and have it in turn converted as base64 string in the end. Buffer.from(content).toString('base64') converts just the string to base64 but not as file

Use 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'));

for more information see this link https://www.npmjs.com/package/create-html

The Node file system API allows you to easily perform such actions:

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'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM