# Upload 3+
# Interface Declaration
{ "name": "system.uploadtask" }
# Import Module
import uploadtask from '@system.uploadtask'
// or
const uploadtask = require('@system.uploadtask')
# Interface Definition
# Methods
# UploadTask uploadtask.uploadFile(OBJECT)
Creates an upload request. Each successful call to uploadtask.uploadFile returns an UploadTask instance for the current request.
# Parameters:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
| url | String | Yes | Developer server URL |
| filePath | String | Yes | Path to the file resource to upload (local path) |
| name | String | Yes | Key corresponding to the file. The developer can use this key to retrieve the binary content of the file on the server side |
| header | Object | No | Request headers. All properties will be set in the request header section |
| formData | Object | No | Additional form data in the HTTP request |
| timeout | Number | No | Timeout duration in milliseconds |
| success | Function | No | Callback function for successful response |
| fail | Function | No | Callback function for failed response |
| complete | Function | No | Callback function for completion (called on both success and failure) |
# success Return Value:
| Parameter Name | Type | Description |
|---|---|---|
| statusCode | Integer | Server status code |
| data | String | Data returned by the developer server |
| headers | Object | All headers from the server response |
# UploadTask
# Methods
# UploadTask.abort()
Aborts the upload task.
# UploadTask.onProgressUpdate(callback)
Listens for upload progress change events.
# Parameters:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
| callback | Function | Yes | Callback function for upload progress change events |
# callback Return Value:
| Parameter Name | Type | Description |
|---|---|---|
| progress | Number | Upload progress percentage |
| totalBytesSent | Number | Length of data already uploaded, in Bytes |
| totalBytesExpectedToSend | Number | Expected total length of data to upload, in Bytes |
# UploadTask.offProgressUpdate(function callback)
Cancels listening for upload progress change events. The callback is optional; if not provided, all upload progress change events listened to via onProgressUpdate will be canceled.
# Example:
const retUploadTask = uploadtask.uploadFile({
url: 'http://www.example.com',
filePath: "internal://mass/download/test.png",
name: "testImg",
success: function(res){
console.log("Upload success.resp = " + JSON.stringify(res))
},
fail: function(data, code) {
console.log(`handling fail, errMsg = ${data)}`)
console.log(`handling fail, errCode = ${code}`)
}
})
// Abort the request task
retUploadTask.abort()
// Listen for upload progress events
retUploadTask.onProgressUpdate(res => {
console.log(
`listening upload progress update event, progressUpdate data = ${JSON.stringify(res)}`
)
})
// Cancel listening for upload progress events
retUploadTask.offProgressUpdate()
Cancel a specific upload progress event:
function cb(res) {
console.log(
`listening for upload progress update event 1, progressUpdate data = ${JSON.stringify(
res
)}`
)
}
// This listener will be canceled
retUploadTask.onProgressUpdate(cb)
// event2 listener remains active and will not be canceled
retUploadTask.onProgressUpdate((res) => {
console.log(
`listening for upload progress update event 2, progressUpdate data = ${JSON.stringify(
res
)}`
)
})
retUploadTask.offProgressUpdate(cb)
# Support Details
| Device Product | Description |
|---|---|
| Xiaomi S1 Pro Fitness Watch | Not supported |
| Xiaomi Smart Band 8 Pro | Not supported |
| Xiaomi Smart Band 9 / 9 Pro | Not supported |
| Xiaomi Watch S3 | Supported |
| Redmi Watch 4 | Not supported |
| Xiaomi Wearable ECG and Blood Pressure Recorder | Not supported |
| Xiaomi Smart Band 10 | Not supported |
| Xiaomi Watch S4 | Supported |
| REDMI Watch 5 | Supported |