1.0.4 • Published 1 year ago

node-base64-to-file v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Node Base64 to File

npm version npm downloads

node-base64-to-file is a light weight javascript base64 string to file conversion library for nodejs.

Getting Started

Installation

# for npm use:
npm install --save node-base64-to-file

# for yarn use:
yarn add node-base64-to-file

To include node-base64-to-file in your project. use one of these:

// ES6 and later
import base64toFile from 'node-base64-to-file';

// ES5 and older
const base64toFile = require('node-base64-to-file');

Usage

const base64toFile = require("node-base64-to-file");

const base64String =
  'data:image/png;base64,iVBORw0KGgo...';

// create an image with the a given name ie 'image'
try {
  const imagePath = await base64toFile(base64String, { filePath: './uploads', fileName: "image", types: ['png'], fileMaxSize: 3145728 });
  console.log(imagePath)
} catch (error) {
  console.log(error)
}

// create an image with the a random name
try {
  const imagePath = await base64toFile(base64String, { filePath: './uploads', randomizeFileNameLength: 14, types: ['png'], fileMaxSize: 3145728 });
  console.log(imagePath)
} catch (error) {
  console.log(error)
}

// alternative usage
base64toFile(base64String, { filePath: './uploads', fileName: "image", types: ['png'], fileMaxSize: 3145728 }).then(
    (imagePath) => {
      console.log(imagePath)
    },
    (error) => {
      console.log(error.message)
    },
  );
...

Arguments

ArgumentTypeRequiredDescriptionDefault
base64StringstringYesThe base64 representation of a file.
optionsobjectNoAn object containing optional properties to customize the behavior of the base64toFile function.
options.filePathstringNoThe directory path where the file will be saved../uploads
options.fileNamestringNoThe name of the file (excluding the extension). If not specified, a random name will be generated.Random name
options.randomizeFileNameLengthnumberNoThe number of characters in the random name.
options.typesarrayNoAn array of valid file extensions (e.g. 'png', 'jpg', 'pdf').['jpg', 'jpeg', 'png']
options.fileMaxSizenumberNoThe maximum number of bytes that the base64 string can have.

Authors

Aziz ALKAN - azzlkn

License

This project is licensed under the MIT License - see the LICENSE file for details

1.0.2

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.1

2 years ago

0.1.1

3 years ago

0.1.0

3 years ago