目录

Download Task

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "system.downloadtask"}

Module Import

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")

Application Scope

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

API

Description

downloadtask.downloadFile(OBJECT)

Creates a download request. Each time this API is successfully called, a new DownloadTask object is returned.

downloadTask.onProgressUpdate(function callback)

Listens to download progress change events.

downloadTask.offProgressUpdate(function callback)

Cancels listening to download progress change events.

downloadTask.onHeadersReceived(function callback)

Listens to HTTP response header events.

downloadTask.offHeadersReceived(OBJECT)

Cancels listening to HTTP response header events.

downloadTask.abort()

Suspends a download task.

[h2]downloadtask.downloadFile(OBJECT) (1078+, Not Formulated by the Vendor Alliance)

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:

  • File name: The file name can contain only letters, digits, and underscores (_).
  • File name extension: The file name extension must be standard, which can contain only letters and digits, for example, txt and mp4.

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))
    }
})

[h2]downloadTask.onProgressUpdate(function callback) (1078+, Not Formulated by the Vendor Alliance)

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))
})

[h2]downloadTask.offProgressUpdate(function callback) (1078+, Not Formulated by the Vendor Alliance)

Description

Cancels listening to download progress change events.

Parameters

Callback function of download progress change events.

Sample code

this.download_task.offProgressUpdate()

[h2]downloadTask.onHeadersReceived(function callback) (1078+, Not Formulated by the Vendor Alliance)

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))
})

[h2]downloadTask.offHeadersReceived(OBJECT) (1078+, Not Formulated by the Vendor Alliance)

Description

Cancels listening to HTTP response header events.

Parameters

Callback function of HTTP response header events.

Sample code

this.download_task.offHeadersReceived()

[h2]downloadTask.abort() (1078+, Not Formulated by the Vendor Alliance)

Description

Suspends a download task.

Sample code

this.download_task.abort()

Links

[h2]Cases

Error Code 300 Is Returned When the file.delete API Fails to Delete a File