An open API service indexing awesome lists of open source software.

https://github.com/Cap-go/capacitor-downloader

Capacitor plugin to download file in background or foreground
https://github.com/Cap-go/capacitor-downloader

capacitor capacitor-plugin plugin

Last synced: 4 months ago
JSON representation

Capacitor plugin to download file in background or foreground

Awesome Lists containing this project

README

          

# @capgo/capacitor-downloader
Capgo - Instant updates for capacitor


➡️ Get Instant updates for your App with Capgo


Missing a feature? We’ll build the plugin for you 💪


Download file in background or foreground

## Why Capacitor Downloader?

A reliable **native file downloader** built for Capacitor apps that continues downloads even when your app is backgrounded or closed:

- **Background downloads** - Downloads continue even when app is minimized, backgrounded, or closed (main advantage)
- **Progress tracking** - Real-time progress updates during downloads
- **Network control** - Choose between WiFi-only or cellular network downloads
- **Custom headers** - Add authentication tokens and custom HTTP headers
- **Resumable downloads** - Pause and resume downloads (platform dependent)
- **Event listeners** - Monitor download progress, completion, and failures
- **Large file support** - Handle multi-gigabyte files without memory issues
- **Free and open source** - No paid services required

Perfect for downloading large media files, offline content, app updates, and any scenario where downloads need to survive app lifecycle events.

## Documentation

The most complete doc is available here: https://capgo.app/docs/plugins/downloader/

## Compatibility

| Plugin version | Capacitor compatibility | Maintained |
| -------------- | ----------------------- | ---------- |
| v8.\*.\* | v8.\*.\* | ✅ |
| v7.\*.\* | v7.\*.\* | On demand |
| v6.\*.\* | v6.\*.\* | ❌ |
| v5.\*.\* | v5.\*.\* | ❌ |

> **Note:** The major version of this plugin follows the major version of Capacitor. Use the version that matches your Capacitor installation (e.g., plugin v8 for Capacitor 8). Only the latest major version is actively maintained.

## Install

```bash
npm install @capgo/capacitor-downloader
npx cap sync
```

## API

* [`download(...)`](#download)
* [`pause(...)`](#pause)
* [`resume(...)`](#resume)
* [`stop(...)`](#stop)
* [`checkStatus(...)`](#checkstatus)
* [`getFileInfo(...)`](#getfileinfo)
* [`addListener('downloadProgress', ...)`](#addlistenerdownloadprogress-)
* [`addListener('downloadCompleted', ...)`](#addlistenerdownloadcompleted-)
* [`addListener('downloadFailed', ...)`](#addlistenerdownloadfailed-)
* [`removeAllListeners()`](#removealllisteners)
* [`getPluginVersion()`](#getpluginversion)
* [Interfaces](#interfaces)

Capacitor plugin for downloading files with background support.
Provides resumable downloads with progress tracking.

### download(...)

```typescript
download(options: DownloadOptions) => Promise
```

Start a new download task.

| Param | Type | Description |
| ------------- | ----------------------------------------------------------- | ------------------------ |
| **`options`** | DownloadOptions | - Download configuration |

**Returns:** Promise<DownloadTask>

--------------------

### pause(...)

```typescript
pause(options: { id: string; }) => Promise
```

Pause an active download.
Download can be resumed later from the same position.

| Param | Type | Description |
| ------------- | ---------------------------- | ----------------------------------------- |
| **`options`** | { id: string; } | - Options containing the download task ID |

--------------------

### resume(...)

```typescript
resume(options: { id: string; }) => Promise
```

Resume a paused download.
Continues from where it was paused.

| Param | Type | Description |
| ------------- | ---------------------------- | ----------------------------------------- |
| **`options`** | { id: string; } | - Options containing the download task ID |

--------------------

### stop(...)

```typescript
stop(options: { id: string; }) => Promise
```

Stop and cancel a download permanently.
Downloaded data will be deleted.

| Param | Type | Description |
| ------------- | ---------------------------- | ----------------------------------------- |
| **`options`** | { id: string; } | - Options containing the download task ID |

--------------------

### checkStatus(...)

```typescript
checkStatus(options: { id: string; }) => Promise
```

Check the current status of a download.

| Param | Type | Description |
| ------------- | ---------------------------- | ----------------------------------------- |
| **`options`** | { id: string; } | - Options containing the download task ID |

**Returns:** Promise<DownloadTask>

--------------------

### getFileInfo(...)

```typescript
getFileInfo(options: { path: string; }) => Promise<{ size: number; type: string; }>
```

Get information about a downloaded file.

| Param | Type | Description |
| ------------- | ------------------------------ | ---------------------------------- |
| **`options`** | { path: string; } | - Options containing the file path |

**Returns:** Promise<{ size: number; type: string; }>

--------------------

### addListener('downloadProgress', ...)

```typescript
addListener(eventName: 'downloadProgress', listenerFunc: (progress: { id: string; progress: number; }) => void) => Promise
```

Listen for download progress updates.
Fired periodically as download progresses.

| Param | Type | Description |
| ------------------ | --------------------------------------------------------------------- | ------------------------------------- |
| **`eventName`** | 'downloadProgress' | - Must be 'downloadProgress' |
| **`listenerFunc`** | (progress: { id: string; progress: number; }) => void | - Callback receiving progress updates |

**Returns:** Promise<PluginListenerHandle>

--------------------

### addListener('downloadCompleted', ...)

```typescript
addListener(eventName: 'downloadCompleted', listenerFunc: (result: { id: string; }) => void) => Promise
```

Listen for download completion.
Fired when a download finishes successfully.

| Param | Type | Description |
| ------------------ | ------------------------------------------------- | -------------------------------------------- |
| **`eventName`** | 'downloadCompleted' | - Must be 'downloadCompleted' |
| **`listenerFunc`** | (result: { id: string; }) => void | - Callback receiving completion notification |

**Returns:** Promise<PluginListenerHandle>

--------------------

### addListener('downloadFailed', ...)

```typescript
addListener(eventName: 'downloadFailed', listenerFunc: (error: { id: string; error: string; }) => void) => Promise
```

Listen for download failures.
Fired when a download encounters an error.

| Param | Type | Description |
| ------------------ | --------------------------------------------------------------- | -------------------------------------- |
| **`eventName`** | 'downloadFailed' | - Must be 'downloadFailed' |
| **`listenerFunc`** | (error: { id: string; error: string; }) => void | - Callback receiving error information |

**Returns:** Promise<PluginListenerHandle>

--------------------

### removeAllListeners()

```typescript
removeAllListeners() => Promise
```

Remove all event listeners.
Cleanup method to prevent memory leaks.

--------------------

### getPluginVersion()

```typescript
getPluginVersion() => Promise<{ version: string; }>
```

Get the plugin version number.

**Returns:** Promise<{ version: string; }>

--------------------

### Interfaces

#### DownloadTask

Represents the current state and progress of a download task.

| Prop | Type | Description |
| -------------- | -------------------------------------------------------------------- | --------------------------------------- |
| **`id`** | string | Unique identifier for the download task |
| **`progress`** | number | Download progress from 0 to 100 |
| **`state`** | 'PENDING' \| 'RUNNING' \| 'PAUSED' \| 'DONE' \| 'ERROR' | Current state of the download |

#### DownloadOptions

Configuration options for starting a download.

| Prop | Type | Description |
| ----------------- | ---------------------------------------- | ------------------------------------------------ |
| **`id`** | string | Unique identifier for this download task |
| **`url`** | string | URL of the file to download |
| **`destination`** | string | Local file path where the download will be saved |
| **`headers`** | { [key: string]: string; } | Optional HTTP headers to include in the request |
| **`network`** | 'cellular' \| 'wifi-only' | Network type requirement for download |
| **`priority`** | 'high' \| 'normal' \| 'low' | Download priority level |

#### PluginListenerHandle

| Prop | Type |
| ------------ | ----------------------------------------- |
| **`remove`** | () => Promise<void> |

### Credit

This plugin was inspired from: https://github.com/kesha-antonov/react-native-background-downloader