2.0.0 • Published 2 years ago

capacitor-plugin-filedownload v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

capacitor-plugin-filedownload

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(options: FileDownloadOptions) => Promise<FileDownloadResponse>
ParamType
optionsFileDownloadOptions

Returns: Promise<FileDownloadResponse>


cancel()

cancel() => Promise<void>

cancel download


isCanceled()

isCanceled() => Promise<CancelStatus>

get status of download

Returns: Promise<CancelStatus>


checkPermissions()

checkPermissions() => Promise<PermissionStatus>

only for android

Returns: Promise<PermissionStatus>


requestPermissions()

requestPermissions() => Promise<PermissionStatus>

only for android

Returns: Promise<PermissionStatus>


openSetting()

openSetting() => Promise<void>

open app setting, only for android


addListener('downloadProgress', ...)

addListener(eventName: 'downloadProgress', listenerFunc: (progress: FileDownloadProgress) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
ParamType
eventName'downloadProgress'
listenerFunc(progress: FileDownloadProgress) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


Interfaces

FileDownloadResponse

PropType
pathstring

FileDownloadOptions

PropTypeDescriptionDefault
urlstring
fileNamestring
destinationDestinationDownload file destinationios default: Document android default: External Storage
headersRecord<string, string>request headers, when headers has value, url must be a http request with POST method
bodyRecord<string, unknown>request body, when body has value, url must be a http request width POST method
downloadTitlestringDownloader Title, Only Android
downloadDescriptionstringDownloader Description, Only Android

CancelStatus

PropType
isCanceledboolean

PermissionStatus

PropTypeDescription
storagePermissionStateprompt: 首次申请,询问。 prompt-with-rationale: 每次都询问。 granted: 已获取权限。 denied:权限已拒绝。

PluginListenerHandle

PropType
remove() => Promise<void>

FileDownloadProgress

PropType
progressnumber

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

{

}

PermissionState

'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'

2.0.0

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago