npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

base64-media-helper

v1.1.0

Published

Utility for converting media files to base64, URLs, and vice versa

Readme

base64-media-helper 🚀

A powerful, lightweight TypeScript library for seamless media file conversions between Base64, Files, and URLs.


🌟 Features

  • 📁 Convert files to Base64 effortlessly.
  • 🔄 Convert Base64 strings back to files.
  • 🌐 Download files from URLs.
  • 🔗 Create object URLs from files.
  • 💻 Full TypeScript support.
  • 🌍 Works with both browsers and Node.js.

📦 Installation

Install the library via npm:

npm install base64-media-helper

🚀 Quick Start

Import the Library

import MediaConverter from 'base64-media-helper';

📘 Usage Examples

1. File to Base64 Conversion

// Convert a file to a Base64 string
const fileInput = document.querySelector('input[type="file"]');
fileInput?.addEventListener('change', async (e) => {
  const file = (e.target as HTMLInputElement).files?.[0];
  if (file) {
    try {
      const base64String = await MediaConverter.fileToBase64(file);
      console.log(base64String);
    } catch (error) {
      console.error('Conversion failed', error);
    }
  }
});

2. Base64 to File Conversion

// Convert Base64 string back to a file
const base64String = 'data:image/png;base64,...'; // Your Base64 string
const file = MediaConverter.base64ToFile(
  base64String,
  'converted-image.png',
  'image/png'
);
console.log(file);

3. URL to File Conversion

// Download a file from a URL
const imageUrl = 'https://example.com/image.jpg';
try {
  const file = await MediaConverter.urlToFile(imageUrl, 'downloaded-image.jpg');
  console.log(file);

  // Optional: Create an object URL to display
  const objectUrl = MediaConverter.fileToUrl(file);
  document.getElementById('previewImage').src = objectUrl;
} catch (error) {
  console.error('URL conversion failed', error);
}

4. File to Object URL

// Create a displayable URL from a file
const file = new File(['Hello, world!'], 'example.txt', { type: 'text/plain' });
const objectUrl = MediaConverter.fileToUrl(file);
console.log(objectUrl);

API Methods

fileToBase64(file: File): Promise<string>

  • Converts a File object to a Base64 string.
  • Returns a Promise resolving to the Base64 representation.

base64ToFile(base64: string, filename: string, mimeType: string): File

  • Converts a Base64 string back to a File object.
  • Requires filename and MIME type.
  • Returns a new File instance.

urlToFile(url: string, filename?: string): Promise<File>

  • Downloads a file from a given URL.
  • Optional filename (defaults to the URL's filename).
  • Returns a Promise resolving to a File object.

fileToUrl(file: File): string

  • Creates an object URL for a given file.
  • Returns a string URL that can be used for displaying or downloading.

🛡️ Error Handling

Each method includes built-in error handling. Use try-catch blocks or .catch() with Promises to manage potential conversion errors.

try {
  const base64 = await MediaConverter.fileToBase64(file);
} catch (error) {
  // Handle conversion errors
  console.error('Conversion failed:', error);
}

🔍 Browser Compatibility

Supports modern browsers with:

  • File
  • FileReader
  • Blob
  • fetch

Compatible with ES6+ environments.

💡 Tips

  • Check file sizes for large media conversions.
  • Use MIME types accurately for proper file handling.
  • Be mindful of memory usage when working with large files.

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

This project is licensed under the MIT License.

🌈 Author

Dinesh Basnet
Happy coding! 🚀