has.uploadFile(Object object): UploadTask
Uploads local resources to the server.
Since version 1.0.4, an UploadTask object is returned when this API is called.
The API address of the developer server must use the HTTPS protocol and the server domain name must be configured in AppGallery Connect.
Since: 1.0.0
Required permissions: ohos.permission.INTERNET, which must be declared in module.json5.
Precautions: Before calling this API, you need to configure the server domain name.
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| url | string | Yes | Address of your server. |
| filePath | string | Yes | Local path for storing uploaded files. |
| name | string | Yes | Key of a file. You can use this key on the server to obtain the file's binary content. |
| header | Object | No | Header of an HTTP request. The header cannot contain a Referer field. |
| formData | Object | No | Additional form data in an HTTP request. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Return value of success
| Parameter | Type | Description |
|---|---|---|
| data | string | Data returned by your server. |
| statusCode | number | HTTP status code returned by your server. |
Returns:
An UploadTask object, which can be used to listen for upload progress change events or upload cancellation events.
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: 1.url format is invalid. 2.filePath is not user data path. |
| 201 | Permission denied. |
| 401 | Parameter error. Possible causes: 1. Missing mandatory parameters. 2. Incorrect parameter type. 3. Parameter verification failed. |
| 40000301 | uploadFile error. For details, see File Management Error Codes. |
Example:
has.uploadFile({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
filePath: 'internal://tmp/xxx.txt',
name: 'data',
success: (res) => {
console.info('uploadFile success', res);
},
fail: (err) => {
console.error('uploadFile fail', err);
},
complete: (res) => {
console.info('uploadFile complete', res);
}
}); UploadTask.abort()
Stops an upload task.
Since: 1.0.4
Error code information
| Error Code | Error Message |
|---|---|
| 40000301 | uploadFile error, error is upload abort. For details, see File Management Error Codes. |
Example:
const uploadTask = has.uploadFile({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
filePath: 'internal://tmp/xxx.txt',
name: 'data',
success: (res) => {
console.info('uploadFile success', res);
},
fail: (err) => {
console.error('uploadFile fail', err);
},
complete: (res) => {
console.info('uploadFile complete', res);
}
});
uploadTask.abort(); UploadTask.onProgressUpdate(function callback)
Enables listening for upload progress change events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for upload progress change events. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| progress | number | Upload progress, in percentage. |
| totalBytesSent | number | Size of the uploaded data, in bytes. |
| totalBytesExpectedToSend | number | Anticipated total size of data to be uploaded, in bytes. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
Example:
const uploadTask = has.uploadFile({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
filePath: 'internal://tmp/xxx.txt',
name: 'data',
success: (res) => {
console.info('uploadFile success', res);
},
fail: (err) => {
console.error('uploadFile fail', err);
},
complete: (res) => {
console.info('uploadFile complete', res);
}
});
const callback = (res) => {
console.info('onProgressUpdate', res);
};
uploadTask.onProgressUpdate(callback); UploadTask.offProgressUpdate(function callback)
Cancels listening for upload progress change events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by onProgressUpdate. If this parameter is not passed, all listener functions will be removed. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
Example:
const uploadTask = has.uploadFile({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
filePath: 'internal://tmp/xxx.txt',
name: 'data',
success: (res) => {
console.info('uploadFile success', res);
},
fail: (err) => {
console.error('uploadFile fail', err);
},
complete: (res) => {
console.info('uploadFile complete', res);
}
});
const callback = (res) => {
console.info('onProgressUpdate', res);
};
uploadTask.onProgressUpdate(callback);
uploadTask.offProgressUpdate(callback); UploadTask.onHeadersReceived(function callback)
Listens for HTTP Response Header events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for the HTTP Response Header event. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| header | Object | HTTP response header returned by the server. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
Example:
const uploadTask = has.uploadFile({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
filePath: 'internal://tmp/xxx.txt',
name: 'data',
success: (res) => {
console.info('uploadFile success', res);
},
fail: (err) => {
console.error('uploadFile fail', err);
}
});
const callback = (res) => {
console.info('onHeadersReceived', res);
};
uploadTask.onHeadersReceived(callback); UploadTask.offHeadersReceived(function callback)
Cancels listening for HTTP Response Header events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by onHeadersReceived. If this parameter is not passed, all listener functions will be removed. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
Example:
const uploadTask = has.uploadFile({
url: 'https://www.example.com', // Replace the example URL with the actual URL.
filePath: 'internal://tmp/xxx.txt',
name: 'data',
success: (res) => {
console.info('uploadFile success', res);
},
fail: (err) => {
console.error('uploadFile fail', err);
}
});
const callback = (res) => {
console.info('onHeadersReceived', res);
};
uploadTask.onHeadersReceived(callback);
uploadTask.offHeadersReceived(callback);