A React hook for effortlessly converting Data URLs / Blob files into File objects, complete with a preview URL for easy viewing and uploading.
You can install the package using npm, Yarn, pnpm, or Bun:
npm install use-blob-to-file
yarn add use-blob-to-file
pnpm add use-blob-to-file
bun add use-blob-to-file
Import and use the useBlobToFile
hook in your React components:
import React from 'react';
import useBlobToFile from 'use-blob-to-file';
const MyComponent = () => {
const dataUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA...'; // Replace with your Data URL
const filename = 'my-image.png';
const { file, previewUrl } = useBlobToFile({ dataUrl, filename });
return (
<div>
<img src={previewUrl} alt="Preview" />
<a href={previewUrl} download={filename}>Download File</a>
</div>
);
};
export default MyComponent;
- Convert Data URLs to File Objects: Easily convert base64-encoded Data URLs into File objects.
- Preview URL: Generate a preview URL to view the file before uploading.
- TypeScript Support: Fully typed with TypeScript for a better development experience.
const useBlobToFile = (args: DataUrlToFileProps) => {
// Returns an object with `file` and `previewUrl`
}
args
(DataUrlToFileProps
): The parameters for conversion.dataUrl
(string): The Data URL to convert.filename
(string): The name of the file to be created.
file
(File
): The created File object.previewUrl
(string
): A URL for previewing the file.
This package is licensed under the MIT License. See the LICENSE file for details.