Gathering detailed insights and metrics for js-base64-file
Gathering detailed insights and metrics for js-base64-file
Gathering detailed insights and metrics for js-base64-file
Gathering detailed insights and metrics for js-base64-file
node loading, converting and saving of local and remote files as base64 images or files. Perfect for image data uri use!
npm install js-base64-file
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
6 Stars
26 Commits
1 Forks
3 Watching
4 Branches
2 Contributors
Updated on 24 Apr 2022
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-41%
552
Compared to previous day
Last week
-8.8%
4,083
Compared to previous week
Last month
-50.5%
17,471
Compared to previous month
Last year
4,818.9%
200,642
Compared to previous year
3
2
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 |
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.
1 2 import {Base64File} from 'js-base64-file'; 3 4 const image=new Base64File; 5 const file='test.png'; 6 const path='./'; 7 8 9 //this will load and convert if needed synchriouniously 10 const data=image.loadSync(path,file); 11 12 console.log(` 13 14 SYNC: you could send this image via ws or http to the browser or save it to disk now : 15 16 ${data.slice(0,50)} ... ${data.slice(-50)} 17 18 19 `); 20
1 2 import {Base64File} from 'js-base64-file'; 3 4 const image=new Base64File; 5 const file='test.png'; 6 const path='./'; 7 8 9 //this will load and convert if needed synchriouniously 10 image.load(path,file,asycHandler); 11 12 function asycHandler(err,data){ 13 if(err){ 14 console.trace(err); 15 } 16 17 console.log(` 18 19 ASYNC: you could send this image via ws or http to the browser or save it to disk now : 20 21 ${data.slice(0,50)} ... ${data.slice(-50)} 22 23 24 `); 25 } 26
1 2 import {Base64File} from 'js-base64-file'; 3 4 const image=new Base64File; 5 6 const remoteURL='https://octodex.github.com/images/'; 7 const remoteFile='megacat-2.png'; 8 const data=await image.loadRemote(remoteURL,remoteFile); 9 10 console.log(` 11 12 REMOTE: you could send this image via ws or http to the browser or save it to disk now : 13 14 ${data.slice(0,50)} ... ${data.slice(-50)} 15 16 17 `); 18
1 2 import {Base64File} from '../index.js'; 3 import {existsSync} from 'fs'; 4 5 const image=new Base64File; 6 7 const remoteURL='https://octodex.github.com/images/'; 8 const remoteFile='megacat-2.png'; 9 10 const localPath='./'; 11 12 //loading an image 13 const data=await image.loadRemote(remoteURL,remoteFile); 14 15 //saving the image 16 image.saveSync(data,localPath,remoteFile); 17 18 console.log(existsSync(localPath+remoteFile)); 19
1 2 import {Base64File} from 'js-base64-file'; 3 import {existsSync} from 'fs'; 4 5 const image=new Base64File; 6 const file='test.png'; 7 const path='./'; 8 const copyAsyncFile=`copy-async-${file}`; 9 10 11 //load a file 12 const data=image.loadSync(path,file); 13 14 //save the file 15 image.save( 16 data, 17 path, 18 copyAsyncFile, 19 function(err,data){ 20 if (err||!existsSync(`${path}${copyAsyncFile}`)){ 21 test.fail(); 22 } 23 24 console.log(existsSync(path+copyAsyncFile)); 25 26 //call next test here 27 } 28 ); 29
It's that simple! And it will work with any file type!
No vulnerabilities found.
No security vulnerabilities found.