Add the following configuration to the features attribute in the manifest.json file:
{"name": "system.downloadtask"} Add the following configuration to the script element on the page where the API will be called:
import downloadtask from '@system.downloadtask'
Or
var downloadtask = require("@system.downloadtask") Item | Description |
|---|---|
Applicable devices | Mobile phones and tablets |
Applicable regions | Global |
API | Description |
|---|---|
Creates a download request. Each time this API is successfully called, a new DownloadTask object is returned. | |
Listens to download progress change events. | |
Cancels listening to download progress change events. | |
Listens to HTTP response header events. | |
Cancels listening to HTTP response header events. | |
Suspends a download task. |
Description
Creates a download request. Each time this API is successfully called, a new DownloadTask object is returned.
Parameters
Parameter | Type | Mandatory/Optional | Description |
|---|---|---|---|
url | string | Mandatory | Resource URL. |
header | object | Optional | Request header, which accommodates all attributes of the request. The setting of useragent is invalid. |
filename | string | Optional | Download file name, which is obtained from the network request or URL by default. The value can be:
|
timeout | number | Optional | Connection timeout interval, but not the read/write timeout interval. The unit is millisecond and the default value is 10000. |
success | function | Optional | Callback upon success. |
fail | function | Optional | Callback upon failure. |
complete | function | Optional | Callback upon completion, regardless of whether the API call is successful or fails. |
Parameters returned in success
Parameter | Type | Description |
|---|---|---|
statusCode | number | Server status code. |
filePath | string | User file path, which must be a local path. |
header | object | All headers in the response from the server. |
Parameters returned in fail
Parameter | Type | Description |
|---|---|---|
msg | object | Error information, which is obtained using msg.errMsg. |
code | number | Result code that is returned. |
Sample code
this.download_task = downloadtask.downloadFile({
url: 'https://downloadtext.com',/*The URL is only an example.*/
success(res) {
console.log("Download success. resp = " + JSON.stringify(res))
},
fail(res) {
console.log("Download fail. resp = " + JSON.stringify(res))
}
}) Description
Listens to download progress change events.
Parameters
Callback function of download progress change events.
Parameters returned in callback
Object type.
Parameter | Type | Description |
|---|---|---|
progress | number | Download progress, in percentage. |
totalBytesWritten | number | Size of the downloaded data, in bytes. |
totalBytesExpectedToWrite | number | Anticipated size of all data to be downloaded, in bytes. |
Sample code
this.download_task.onProgressUpdate(res => {
console.log("Download onProgressUpdate = " + JSON.stringify(res))
}) Description
Cancels listening to download progress change events.
Parameters
Callback function of download progress change events.
Sample code
this.download_task.offProgressUpdate()
Description
Listens to HTTP response header events. This API is called before the file download success callback.
Parameters
callback: callback function of HTTP response header events.
Parameters returned in callback
Object type.
Parameter | Type | Description |
|---|---|---|
header | object | HTTP response header returned by your server. |
Sample code
this.download_task.onHeadersReceived(res => {
console.log("Download onHeaderReceived = " + JSON.stringify(res))
}) Description
Cancels listening to HTTP response header events.
Parameters
Callback function of HTTP response header events.
Sample code
this.download_task.offHeadersReceived()