useDownloadFile
Package
Share - File
Category
Description
Easily download files from a URL, Blob, Base64, or Buffer (ArrayBuffer/Uint8Array). Provides download status tracking and error handling.
Easily download files from a URL, Blob, Base64, or Buffer (ArrayBuffer/Uint8Array). Provides download status tracking and error handling.
Demo
Status: idle
Source:
Return Values
Name | Description |
---|---|
status | Current download status ('idle' , 'downloading' , 'success' , 'error' ) |
error | Error message if the download fails (or null if no error) |
downloadFile | Unified function to download a file from URL, Blob, base64, or binary buffer |
Input Parameters for downloadFile
data
(string | Blob | ArrayBuffer | Uint8Array)- Required. The content to be downloaded. It can be:
- A URL (string)
- A base64 string (with or without data URI prefix)
- A
Blob
object - An
ArrayBuffer
orUint8Array
for binary data
- Required. The content to be downloaded. It can be:
filename
(string)- Optional. The name of the file to save. Defaults to
'download'
if not provided.
- Optional. The name of the file to save. Defaults to
mimeType
(string)- Optional. The MIME type of the file content.
- If not provided, it will be guessed based on the file extension in
filename
, or default to'application/octet-stream'
.
Usage
ts
import { useDownloadFile } from '@hungpvq/shared-file';
const { status, error, downloadFile } = useDownloadFile();
// Download from URL
await downloadFile('https://example.com/file.pdf', 'example.pdf');
// Download from base64
const base64 = 'data:text/plain;base64,SGVsbG8=';
await downloadFile(base64, 'greeting.txt');
// Download from Blob
const blob = new Blob(['Hello'], { type: 'text/plain' });
await downloadFile(blob, 'hello.txt');
// Download from ArrayBuffer / Uint8Array
const buffer = new TextEncoder().encode('binary data');
await downloadFile(buffer, 'data.bin');
Notes
- Automatically detects input type (URL, base64, Blob, Buffer).
- Automatically guesses MIME type from filename if not provided.
- Supports binary data with
ArrayBuffer
andUint8Array
.