
Security News
New CVE Forecasting Tool Predicts 47,000 Disclosures in 2025
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
js-base64-file
Advanced tools
load fetch convert and save local and remote files and images to base64 in js
This is an ideal way to load and convert local and remote files to base64 either for use as a string or to save on disk.
This works with all file types!
install npm i js-base64-file
method | params | description |
---|---|---|
load | path,fileName,callback | loads a local file and converts it to base64. note : path should always end with a slash |
loadSync | path,fileName | same as load, but it returns the base64 string instead of passing it to a callback. This could be slow on really large files. |
loadRemote | url,fileName,node-fetch options | loads a remote file and converts it to base64. This defaults to a simple GET request, but allows the full options from node-fetch for any type of request even with payloads |
save | data,path,fileName,callback | saves the data to the specified path and filename async callback |
saveSync | data,path,fileName | saves the data to the specified path and filename sync |
callback | params | description |
---|---|---|
load | err, base64Data | gives you the base64 encoded file data |
save | err, data | will pass any errors back it is unlikely there will ever be data |
We have 98% coverage with the amazing C8
coverage tool. The only things not covered are empty default param functions. You can see the coverage details in the ./coverage/index.html
file.
We use the vanilla-test testing module
for super light and fast testing, and have integrated with Travis CI.
You can run the tests and explore the coverage yourself by cloning the repo and running npm test
. It will automatically set everything up and run coverage for you.
You can look at and run the files in the ./example
dir and run them with node ./example/{example}.js
Below are some quick copy paste examples for you.
import {Base64File} from 'js-base64-file';
const image=new Base64File;
const file='test.png';
const path='./';
//this will load and convert if needed synchriouniously
const data=image.loadSync(path,file);
console.log(`
SYNC: you could send this image via ws or http to the browser or save it to disk now :
${data.slice(0,50)} ... ${data.slice(-50)}
`);
import {Base64File} from 'js-base64-file';
const image=new Base64File;
const file='test.png';
const path='./';
//this will load and convert if needed synchriouniously
image.load(path,file,asycHandler);
function asycHandler(err,data){
if(err){
console.trace(err);
}
console.log(`
ASYNC: you could send this image via ws or http to the browser or save it to disk now :
${data.slice(0,50)} ... ${data.slice(-50)}
`);
}
import {Base64File} from 'js-base64-file';
const image=new Base64File;
const remoteURL='https://octodex.github.com/images/';
const remoteFile='megacat-2.png';
const data=await image.loadRemote(remoteURL,remoteFile);
console.log(`
REMOTE: you could send this image via ws or http to the browser or save it to disk now :
${data.slice(0,50)} ... ${data.slice(-50)}
`);
import {Base64File} from '../index.js';
import {existsSync} from 'fs';
const image=new Base64File;
const remoteURL='https://octodex.github.com/images/';
const remoteFile='megacat-2.png';
const localPath='./';
//loading an image
const data=await image.loadRemote(remoteURL,remoteFile);
//saving the image
image.saveSync(data,localPath,remoteFile);
console.log(existsSync(localPath+remoteFile));
import {Base64File} from 'js-base64-file';
import {existsSync} from 'fs';
const image=new Base64File;
const file='test.png';
const path='./';
const copyAsyncFile=`copy-async-${file}`;
//load a file
const data=image.loadSync(path,file);
//save the file
image.save(
data,
path,
copyAsyncFile,
function(err,data){
if (err||!existsSync(`${path}${copyAsyncFile}`)){
test.fail();
}
console.log(existsSync(path+copyAsyncFile));
//call next test here
}
);
It's that simple! And it will work with any file type!
FAQs
load fetch convert and save local and remote files and images to base64 in js
We found that js-base64-file demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.