目录

Upload Task

API Declaration

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

{"name": "system.uploadtask"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import uploadtask from '@system.uploadtask'

Or

var uploadtask = require("@system.uploadtask")

Application Scope

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

API

Description

uploadtask.uploadFile(OBJECT)

Creates an upload request. Each time this API is successfully called, a new UploadTask object is returned.

uploadTask.onProgressUpdate(function callback)

Listens to upload progress change events.

uploadTask.offProgressUpdate(function callback)

Cancels listening to upload progress change events.

uploadTask.onHeadersReceived(function callback)

Listens to HTTP response header events.

uploadTask.offHeadersReceived(OBJECT)

Cancels listening to HTTP response header events.

uploadTask.abort()

Suspends an upload task.

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

Description

Creates an upload request. Each time this API is successfully called, a new UploadTask 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.

method

string

Optional

The options are POST and PUT. The default value is POST.

files

array

Mandatory

List of files to be uploaded, which is submitted through multipart/form-data.

The value of files is an array of file objects. For the structure of the file object, please refer to files parameters.

data

array

Optional

Additional form data in an HTTP request. For details about the data structure, please refer to data parameters.

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.

files parameters

Parameter

Type

Mandatory/Optional

Description

filename

string

Optional

File name in the header when multipart is submitted. If the file name is not specified here, the file name is obtained from the URI by default.

name

string

Optional

Project name of the form when multipart is submitted. The default value is file.

uri

string

Mandatory

Local address of the files to be uploaded.

type

string

Optional

Content-Type of the files to be uploaded. If this parameter is left empty, the system queries the mapping table for Content-Type based on the filename or uri suffix. If Content-Type cannot be obtained, an error message is displayed, indicating that the parameter is invalid.

data parameters

Parameter

Type

Mandatory/Optional

Description

name

string

Mandatory

Name of the form element.

value

string

Mandatory

Value of the form element.

Parameters returned in success

Parameter

Type

Description

statusCode

number

Server status code.

data

string

If the value of type in the header returned by the server is text/*, application/json, application/javascript, or application/xml, the value is text. Otherwise, the value is the URI of a temporary file. If the value is an image or a video, it can be displayed on the image or video control.

headers

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

uploadWithTask() {
    const that = this
    this.upload_task = uploadtask.uploadFile({
        url: "https://uploadtest.com", /*The URL is only an example.*/
        header: { "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,en-US;q=0.8,en;q=0.6" },
        files: [
            {
                uri: "internal://mass/videoupload.mp4",
                name: "file1",
                type: "video"
            }
        ], data: [
            {
                name: "key1",
                value: "value1"
            }
        ],
        success: function (data) {
            console.log("handling success:" + JSON.stringify(data));
        },
        fail: function (data, code) {
            console.log("handling fail, data =" + data.statusText);
        }
    })
},
abort() {
    this.upload_task.abort()
},

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

Description

Listens to upload progress change events.

Parameters

Callback function of upload progress change events.

Parameters returned in callback

Object type.

Parameter

Type

Description

progress

number

Upload progress, in percentage.

totalBytesSent

number

Size of the uploaded data, in bytes.

totalBytesExpectedToSend

number

Anticipated size of all data to be uploaded, in bytes.

Sample code

this.upload_task.onProgressUpdate(res => {
    console.log("Upload onProgressUpdate = " + JSON.stringify(res))
    that.progress = JSON.stringify(res)
})

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

Description

Cancels listening to upload progress change events.

Parameters

Callback function of upload progress change events.

Sample code

this.upload_task.offProgressUpdate()

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

Description

Listens to HTTP response header events. This API is called before the file upload success callback.

Parameters

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.upload_task.onHeadersReceived(res => {
    console.log("Upload onHeaderReceived = " + JSON.stringify(res))
    that.header = JSON.stringify(res)
})

[h2]uploadTask.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.upload_task.offHeadersReceived()

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

Description

Suspends an upload task.

Sample code

this.upload_task.abort()