load fetch convert and save local and remote files and images to base64 in js
npm install js-base64-file npm i js-base64-file
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|
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.
vanilla-test testing module for super light and fast testing, and have integrated with Travis CI.
npm test. It will automatically set everything up and run coverage for you.
./example dir and run them with node ./example/{example}.js
javascript
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(
);
`
Load Async callback
`javascript
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(
);
}
`
Load Remote Await
`javascript
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(
);
`
SaveSync
`javascript
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));
`
Save Async Callback
`javascript
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
}
);
``