Property | Type | Required | Description |
url | string | True | The URL of the resource to download. |
header | Object | False | HTTP request header. The Referer cannot be set in the header. |
filePath | string | False | The path where the downloaded file will be stored. |
success | function | False | Callback function for successful API calls. |
fail | function | False | Callback function for failed API calls. |
complete | function | False | Callback function executed after API call ends (regardless of success or failure). |
Property | Type | Description |
tempFilePath | string | Temporary file path (local path). Returned if filePath is not specified. The downloaded file will be stored in a temporary file |
filePath | string | User file path (local path). Returned if filePath is specified and will match the provided filePath. |
statusCode | number | The HTTP status code returned by the developer’s server. |
wx.downloadFile({url: 'https://example.com/audio/123', // Example URL, not a real resourcesuccess (res) {// As long as the server responds, the response content will be written to the file and the success callback will be triggered. You need to determine if the desired content was downloadedif (res.statusCode === 200) {console.log('filePath', res.tempFilePath);}}})
This method is called using DownloadTask.abort().
This method is called using DownloadTask.onProgressUpdate(function listener).
Property | Type | Description |
progress | number | Download progress percentage. |
totalBytesWritten | number | The length of data that has been downloaded, in bytes. |
totalBytesExpectedToWrite | number | The total length of data expected to be downloaded, in bytes. |
This method is called using DownloadTask.offProgressUpdate(function listener).
const listener = function (res) { console.log(res) }DownloadTask.onHeadersReceived(listener)DownloadTask.offHeadersReceived(listener) // Must pass the same function object used in onHeadersReceived
This method is called using DownloadTask.onHeadersReceived(function listener).
Property | Type | Description |
header | Object | The HTTP response header returned by the developer’s server. |
This method is called using DownloadTask.offHeadersReceived(function listener).
const listener = function (res) { console.log(res) }DownloadTask.onHeadersReceived(listener)DownloadTask.offHeadersReceived(listener) // Must pass the same function object used in onHeadersReceived
const downloadTask = wx.downloadFile({url: 'http://example.com/audio/123', // Example URL, not a real resourcesuccess (res) {wx.playVoice({filePath: res.tempFilePath})}})downloadTask.onProgressUpdate((res) => {console.log ('Download progress', res.progress)console.log (' Length of the downloaded data', res.totalBytesWritten)console.log ('Expected total length of the data to be downloaded', res.totalBytesExpectedToWrite)})DownloadTask.abort () // Cancel the download task