tencent cloud

All product documents
Tencent Cloud Super App as a Service
Download
Last updated: 2025-04-10 18:18:33
Download
Last updated: 2025-04-10 18:18:33

downloadFile

This API is called using DownloadTask wx.downloadFile(Object object).
Note:
Please specify a reasonable Content-Type field in the server response header to ensure that the client processes the file type correctly.
Feature description:Downloads a file resource to the local device. The client initiates an HTTPS GET request, with the local temporary path (local path) of the file returned. The maximum file size allowed for a single download is 200 MB. Before calling this API, please read Network.
Parameter and description:Object object.
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).
object.success callback function parameter:Object res.
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.
Return value:DownloadTask
Example:
wx.downloadFile({
url: 'https://example.com/audio/123', // Example URL, not a real resource
success (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 downloaded
if (res.statusCode === 200) {
console.log('filePath', res.tempFilePath);
}
}
})

DownloadTask

.abort

This method is called using DownloadTask.abort().
Feature description:Aborts the download task.

.onProgressUpdate

This method is called using DownloadTask.onProgressUpdate(function listener).
Feature description:Listens for download progress change event.
Parameter and description:function listener. The listener for download progress change event.
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.

.offProgressUpdate

This method is called using DownloadTask.offProgressUpdate(function listener).
Feature description:Removes the listener for download progress change event.
Parameter and description:function listener. The listener passed to onProgressUpdate. If not provided, all listeners will be removed.
Example:
const listener = function (res) { console.log(res) }

DownloadTask.onHeadersReceived(listener)
DownloadTask.offHeadersReceived(listener) // Must pass the same function object used in onHeadersReceived

.onHeadersReceived

This method is called using DownloadTask.onHeadersReceived(function listener).
Feature description:Listens for the HTTP Response Header event. The event is triggered before the response is received.
Parameter and description:function listener. The listener for the HTTP Response Header event.
Property
Type
Description
header
Object
The HTTP response header returned by the developer’s server.

.offHeadersReceived

This method is called using DownloadTask.offHeadersReceived(function listener).
Feature description:Removes the listener for the HTTP Response Header event.
Parameter and description:function listener. The listener passed to onHeadersReceived. If not provided, all listeners will be removed.
Example:
const listener = function (res) { console.log(res) }

DownloadTask.onHeadersReceived(listener)
DownloadTask.offHeadersReceived(listener) // Must pass the same function object used in onHeadersReceived

Example

const downloadTask = wx.downloadFile({
url: 'http://example.com/audio/123', // Example URL, not a real resource
success (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


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon