a file download plugin for capacitor3.0+
since v2.0.0 supported Capacitor5
since version
1.0.6
, theuri
option was changed tourl
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
FileDownloadOptions
|
**Returns:** Promise<FileDownloadResponse>
--------------------
### cancel()
```typescript
cancel() => PromisePromise<CancelStatus>
--------------------
### checkPermissions()
```typescript
checkPermissions() => PromisePromise<PermissionStatus>
--------------------
### requestPermissions()
```typescript
requestPermissions() => PromisePromise<PermissionStatus>
--------------------
### openSetting()
```typescript
openSetting() => Promise'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'