Download
downloadFile
TIP
The API usage is as follows: DownloadTask wx.downloadFile(Object object)
TIP
Please specify a reasonable Content-Type field in the header of the server response to ensure that the client handles the file type correctly.
Functional description: Download file resources to the local. The client directly initiates an HTTPS GET request and returns the local temporary path (local path) of the file. The maximum file allowed for a single download is 200MB. Please refer to the following before use Related instructions.
Parameters and descriptions: Object object。
Properties Type Required Description url string Yes The url of the downloaded resource header Object No Header of the HTTP request. Referer cannot be set in the header filePath string No Specify the path where the file is stored after downloading success Function No Callback function for successful interface call fail Function No Callback function for failed interface call complete Function No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object res。
Properties Type Description tempFilePath string Temporary file path (local path). If the filePath is not passed in to specify the file storage path, it will return, and the downloaded file will be stored in a temporary file filePath string User file path (local path). If filePath is passed in, it will return, which is consistent with the passed in filePath statusCode number HTTP status code returned by the developer server Return value: DownloadTask
Sample code:
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
})
}
}
})DownloadTask
.abort
TIP
The API usage is as follows: DownloadTask.abort()
- Functional description: Interrupt the download task.
.onProgressUpdate
TIP
The API usage is as follows: DownloadTask.onProgressUpdate(function listener)
Functional description: Listen for download progress change events.
Parameters and descriptions: function listener,Listener function for download progress change events.
Properties Type Description progress number Download progress percentage totalBytesWritten number The length of the data that has been downloaded, in Bytes totalBytesExpectedToWrite number The total length of the data expected to be downloaded, in Bytes
.offProgressUpdate
TIP
The API usage is as follows: DownloadTask.offProgressUpdate(function listener)
Functional description: Remove the listener function for download progress change events
Parameters and descriptions: function listener,onProgressUpdate The listener function passed in. If this parameter is not passed, all listeners will be removed.
Sample code:
const listener = function (res) { console.log(res) }
DownloadTask.onHeadersReceived(listener)
DownloadTask.offHeadersReceived(listener) // Pass the same function object used for listening..onHeadersReceived
TIP
The API usage is as follows: DownloadTask.onHeadersReceived(function listener)
Functional description: Listen to the HTTP Response Header event. It will be earlier than the request completion event.
Parameters and descriptions: function listener,Listener function of the HTTP Response Header event.
Properties Type Description header Object HTTP Response Header returned by the developer server
.offHeadersReceived
TIP
The API usage is as follows: DownloadTask.offHeadersReceived(function listener)
Functional description: Remove the listener function of the HTTP Response Header event.
Parameters and descriptions: function listener,onHeadersReceived The listener function passed in. If this parameter is not passed, all listeners will be removed.
Sample code:
const listener = function (res) { console.log(res) }
DownloadTask.onHeadersReceived(listener)
DownloadTask.offHeadersReceived(listener) // Pass the same function object used for listening.- Sample code:
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