node-base64-to-file
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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

Argument Type Required Description Default
base64String string Yes The base64 representation of a file.
options object No An object containing optional properties to customize the behavior of the base64toFile function.
options.filePath string No The directory path where the file will be saved. ./uploads
options.fileName string No The name of the file (excluding the extension). If not specified, a random name will be generated. Random name
options.randomizeFileNameLength number No The number of characters in the random name.
options.types array No An array of valid file extensions (e.g. ['png', 'jpg', 'pdf']). ['jpg', 'jpeg', 'png']
options.fileMaxSize number No The 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

Package Sidebar

Install

npm i node-base64-to-file

Weekly Downloads

124

Version

1.0.4

License

MIT

Unpacked Size

16.4 kB

Total Files

4

Last publish

Collaborators

  • azzlkn