Attribute | Type | Required | Description |
url | string | Yes | URL for resource download |
header | Object | No | HTTP request's Header; setting Referer within the Header is not allowed. |
filePath | string | No | Designated path for storing the downloaded files |
success | function | No | Callback Function of Successful Interface Call |
fail | function | No | Callback Function of Failing Interface Call |
complete | function | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Attribute | Type | Description |
tempFilePath | string | Temporary file path (local path). If no filePath is specified for file storage, this will be returned. The downloaded files will be stored in a temporary folder. |
filePath | string | User file path (local path). This will be returned when a filePath is passed in, and it will be consistent with the provided filePath. |
statusCode | number | HTTP status code returned from the developer's server. |
wx.downloadFile({url: 'https://example.com/audio/123', // This is merely an example, not an actual resource.success (res) {// As long as the server has response data, the response content will be written into the file and enter the success callback. It is up to the business side to determine whether the desired content has been downloaded.if (res.statusCode === 200) {wx.playVoice({filePath: res.tempFilePath})}}})
This method is used via DownloadTask.abort().
This method is used via DownloadTask.onProgressUpdate(function listener).
Attribute | Type | Description |
progress | number | Download progress percentage |
totalBytesWritten | number | Length of the data already downloaded, measured in Bytes. |
totalBytesExpectedToWrite | number | Total length of the data expected to be downloaded, measured in bytes. |
This method is used via DownloadTask.offProgressUpdate(function listener).
const listener = function (res) { console.log(res) }DownloadTask.onHeadersReceived(listener)DownloadTask.offHeadersReceived(listener) // The same function object as the listener must be passed in.
This method is used via DownloadTask.onHeadersReceived(function listener).
Attribute | Type | Description |
header | Object | HTTP Response Header returned from the developer's server. |
This method is used via DownloadTask.offHeadersReceived(function listener).
const listener = function (res) { console.log(res) }DownloadTask.onHeadersReceived(listener)DownloadTask.offHeadersReceived(listener) // The same function object as the listener must be passed in.
const downloadTask = wx.downloadFile({url: 'http://example.com/audio/123', // This is merely an example, not an actual resource.success (res) {wx.playVoice({filePath: res.tempFilePath})}})downloadTask.onProgressUpdate((res) => {console.log('Download progress', res.progress)console.log('Length of data already downloaded', res.totalBytesWritten)console.log(''Length of data expected to be downloaded', res.totalBytesExpectedToWrite)})downloadTask.abort() // Aborts the download task
Was this page helpful?