AlwaysLoveme / capacitor-plugin-filedownload

a file download plugin for Capacitor3.0+
MIT License
13 stars 7 forks source link

capacitor-plugin-filedownload

a file download plugin for capacitor3.0+

since v2.0.0 supported Capacitor5

since version 1.0.6 , the uri option was changed to url

Install

npm install capacitor-plugin-filedownload
npx cap sync

eg:

import { FileDownload } from "capacitor-plugin-filedownload";

const download = async () => {
  FileDownload.download({
    url: "http://www.xxxxx.com/file/rvh.apk",
    fileName: "release.apk",
    // headers for http request with POST method
    headers: {},
    // parameter for http request with POST method
    body: {},
    // only works on Android, deprecated since 1.0.6
    downloadTitle: 'downloading',
    // only works on Android, deprecated since 1.0.6
    downloadDescription: 'file is downloading',
  }).then((res) => {
    console.log(res.path);
  }).catch(err => {
    console.log(err);
  })
}

// cancel download
const cancelDownload = async () => {
  await FileDownload.cancel();
}

// get download status
const getDownloadStatus = () => {
  const {isCanceled} = await FileDownload.isCanceled();
  console.log(isCanceled);
}

// event listener for downloadProgress
const onDownloadProgress = async () => {
  const eventListener = await FileDownload.addListener('downloadProgress', data =>{
    console.log(data.progress);
  })

  // remove eventListener
  eventListener.remove();
}

...

if you wish to open the file, you can install this plugin: https://github.com/capacitor-community/file-opener

API

* [`download(...)`](#download) * [`cancel()`](#cancel) * [`isCanceled()`](#iscanceled) * [`checkPermissions()`](#checkpermissions) * [`requestPermissions()`](#requestpermissions) * [`openSetting()`](#opensetting) * [`addListener('downloadProgress', ...)`](#addlistenerdownloadprogress) * [Interfaces](#interfaces) * [Type Aliases](#type-aliases) ### download(...) ```typescript download(options: FileDownloadOptions) => Promise ``` | Param | Type | | ------------- | ------------------------------------------------------------------- | | **`options`** | FileDownloadOptions | **Returns:** Promise<FileDownloadResponse> -------------------- ### cancel() ```typescript cancel() => Promise ``` cancel download -------------------- ### isCanceled() ```typescript isCanceled() => Promise ``` get status of download **Returns:** Promise<CancelStatus> -------------------- ### checkPermissions() ```typescript checkPermissions() => Promise ``` only for android **Returns:** Promise<PermissionStatus> -------------------- ### requestPermissions() ```typescript requestPermissions() => Promise ``` only for android **Returns:** Promise<PermissionStatus> -------------------- ### openSetting() ```typescript openSetting() => Promise ``` open app setting, only for android -------------------- ### addListener('downloadProgress', ...) ```typescript addListener(eventName: 'downloadProgress', listenerFunc: (progress: FileDownloadProgress) => void) => Promise & PluginListenerHandle ``` | Param | Type | | ------------------ | -------------------------------------------------------------------------------------------- | | **`eventName`** | 'downloadProgress' | | **`listenerFunc`** | (progress: FileDownloadProgress) => void | **Returns:** Promise<PluginListenerHandle> & PluginListenerHandle -------------------- ### Interfaces #### FileDownloadResponse | Prop | Type | | ---------- | ------------------- | | **`path`** | string | #### FileDownloadOptions | Prop | Type | Description | Default | | ------------------------- | ---------------------------------------------------------------- | ------------------------------------------------------------------------------------ | -------------------------------------------------------------------- | | **`url`** | string | | | | **`fileName`** | string | | | | **`destination`** | Destination | Download file destination | ios default: Document android default: External Storage | | **`headers`** | Record<string, string> | request headers, when headers has value, url must be a http request with POST method | | | **`body`** | Record<string, unknown> | request body, when body has value, url must be a http request width POST method | | | **`downloadTitle`** | string | Downloader Title, Only Android | | | **`downloadDescription`** | string | Downloader Description, Only Android | | #### CancelStatus | Prop | Type | | ---------------- | -------------------- | | **`isCanceled`** | boolean | #### PermissionStatus | Prop | Type | Description | | ------------- | ----------------------------------------------------------- | ---------------------------------------------------------------------------- | | **`storage`** | PermissionState | prompt: 首次申请,询问。 prompt-with-rationale: 每次都询问。 granted: 已获取权限。 denied:权限已拒绝。 | #### PluginListenerHandle | Prop | Type | | ------------ | ----------------------------------------- | | **`remove`** | () => Promise<void> | #### FileDownloadProgress | Prop | Type | | -------------- | ------------------- | | **`progress`** | number | ### Type Aliases #### Destination download destination , on android default is "DOWNLOAD", on ios default is "DOCUMENT" "DOCUMENT" | "EXTERNAL" | "EXTERNAL_STORAGE" | "DATA" | "CACHE" | "LIBRARY" #### Record Construct a type with a set of properties K of type T { [P in K]: T; } #### PermissionState 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'