Skip to main content
Version: v7

@capacitor/file-transfer

The FileTransfer API provides mechanisms for downloading and uploading files.

Installโ€‹

npm install @capacitor/file-transfer
npx cap sync

APIโ€‹

downloadFile(...)โ€‹

downloadFile(options: DownloadFileOptions) => Promise<DownloadFileResult>

Perform an HTTP request to a server and download the file to the specified destination.

ParamType
options
DownloadFileOptions

Returns:

Promise<DownloadFileResult>

Since: 1.0.0


uploadFile(...)โ€‹

uploadFile(options: UploadFileOptions) => Promise<UploadFileResult>

Perform an HTTP request to upload a file to a server

ParamType
options
UploadFileOptions

Returns:

Promise<UploadFileResult>

Since: 1.0.0


addListener('progress', ...)โ€‹

addListener(eventName: "progress", listenerFunc: (progress: ProgressStatus) => void) => Promise<PluginListenerHandle>

Add a listener to file transfer (download or upload) progress events.

ParamType
eventName'progress'
listenerFunc
(progress: ProgressStatus) => void

Returns:

Promise<PluginListenerHandle>

Since: 1.0.0


removeAllListeners()โ€‹

removeAllListeners() => Promise<void>

Remove all listeners for this plugin.

Since: 1.0.0


Interfacesโ€‹

DownloadFileResultโ€‹

PropTypeDescriptionSince
pathstringThe path the file was downloaded to.1.0.0
blobBlobThe blob data of the downloaded file. This is only available on web.1.0.0

DownloadFileOptionsโ€‹

PropTypeDescriptionSince
pathstringThe full file path the downloaded file should be moved to.1.0.0
progressbooleanIf true, progress event will be dispatched on every chunk received. See addListener() for more information. Chunks are throttled to every 100ms on Android/iOS to avoid slowdowns.1.0.0

UploadFileResultโ€‹

PropTypeDescriptionSince
bytesSentnumberTotal number of bytes uploaded1.0.0
responseCodestringHTTP response code for the upload1.0.0
responsestringHTTP response body from the upload (when available)1.0.0
headers{ [key: string]: string; }HTTP headers from the upload response (when available)1.0.0

UploadFileOptionsโ€‹

PropTypeDescriptionSince
pathstringFull file path of the file to upload.1.0.0
blobBlobBlob data to upload. Will use this instead of path if provided. This is only available on web.1.0.0
chunkedModebooleanWhether to upload data in a chunked streaming mode. Not supported on web.1.0.0
mimeTypestringMime type of the data to upload. Only used if "Content-Type" header was not provided.1.0.0
fileKeystringType of form element. The default is set to "file". Only used if "Content-Type" header was not provided.1.0.0
progressbooleanIf true, progress event will be dispatched on every chunk received. See addListener() for more information. Chunks are throttled to every 100ms on Android/iOS to avoid slowdowns.1.0.0

PluginListenerHandleโ€‹

PropType
remove() => Promise<void>

ProgressStatusโ€‹

PropTypeDescriptionSince
type'download' | 'upload'The type of transfer operation (download or upload).1.0.0
urlstringThe url of the file associated with the transfer (download or upload).1.0.0
bytesnumberThe number of bytes transferred so far.1.0.0
contentLengthnumberThe total number of bytes associated with the file transfer.1.0.0
lengthComputablebooleanWhether or not the contentLength value is relevant. In some situations, the total number of bytes may not be possible to determine.1.0.0

Errorsโ€‹

The plugin returns the following errors with specific codes on iOS, Android, and Web:

Error codePlatform(s)Description
OS-PLUG-FLTR-0004Android, iOSThe method's input parameters aren't valid.
OS-PLUG-FLTR-0005Android, iOSInvalid server URL was provided or URL is empty.
OS-PLUG-FLTR-0006Android, iOSUnable to perform operation, user denied permission request.
OS-PLUG-FLTR-0007Android, iOSOperation failed because file does not exist.
OS-PLUG-FLTR-0008Android, iOS, WebFailed to connect to server.
OS-PLUG-FLTR-0009Android, iOSThe server responded with HTTP 304 โ€“ Not Modified. If you want to avoid this, check your headers related to HTTP caching.
OS-PLUG-FLTR-0010Android, iOSThe server responded with an HTTP error status code.
OS-PLUG-FLTR-0011Android, iOS, WebThe operation failed with an error (generic error).

When handling errors in your application, you can check the error code to determine the specific issue. The error object typically contains additional information such as:

  • code: The error code (as shown in the table above)
  • message: A human-readable description of the error
  • source: The source of the transfer (file path or URL)
  • target: The target of the transfer (file path or URL)
  • httpStatus: The HTTP status code (for HTTP errors)
  • body: The response body (for HTTP errors)
  • headers: The response headers (for HTTP errors)