Last active
September 27, 2024 10:59
-
-
Save james2doyle/a4cff8e12456318c71b1 to your computer and use it in GitHub Desktop.
base64 encode a file with node.js and auto-detect the mimetype.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// to run: node node-base64-encode.js file | |
const mime = require('mime'); // npm install mime | |
const path = require('path'); | |
const fs = require('fs'); | |
// path to the file we passed in | |
const filepath = path.resolve(process.argv[2]); | |
// get the mimetype | |
const filemime = mime.getType(filepath); | |
fs.readFile(filepath, {encoding: 'base64'}, (err, data) => { | |
if (err) { | |
throw err; | |
} | |
console.log(`data:${filemime};base64,${data}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the example.
As today, mime. lookup() function is renamed to getType()