Uploading and Downloading
Note:
The APIs of this module are no longer maintained since API version 6. It is recommended that you use
@ohos.request
instead.The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Modules to Import
import request from '@system.request';
Required Permissions
ohos.permission.INTERNET.
request.upload
upload(Object): void
Uploads files.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
url | string | Yes | URL of the upload server. |
header | Object | No | Request header. |
method | string | No | Request methods available: POST and PUT. The default value is POST. |
files | Array<File> | Yes | List of files to upload, which is submitted through multipart/form-data. |
data | Array<RequestData> | No | Form data in the request body. |
success | Function | No | Called when the download task is complete. |
fail | Function | No | Called when downloading fails or the task does not exist. |
complete | Function | No | Called when the execution is complete. |
Table 1 File
Name | Type | Mandatory | Description |
---|---|---|---|
filename | string | No | File name in the header when multipart is used. |
name | string | No | Name of a form item when multipart is used. The default value is file. |
uri | string | Yes | Local storage path of a file. |
type | string | No | Type of the file content. By default, the type is obtained based on the suffix of the file name or URI. |
Table 2 RequestData
Name | Type | Mandatory | Description |
---|---|---|---|
name | string | Yes | Name of the form element |
value | string | Yes | Value of the form element |
When the files are successfully uploaded, the following values will be returned.
Name | Type | Description |
---|---|---|
code | number | HTTP status code returned by the server. |
data | string | Content returned by the server. The value type is determined by the type in the returned headers. |
headers | Object | Headers returned by the server. |
When the files fail to be uploaded, an HTTP status code is returned in code of data.
Example
export default {
upLoad() {
request.upload({
url: 'http://www.path.com',
files: [
{
uri: 'internal://cache/path/to/file.txt',
name: 'file',
filename: 'file.txt',
},
],
data:[
{
name: 'name1',
value: 'value',
},
],
success: function(data) {
console.log('upload success, code:' + data.code);
},
fail: function() {
console.log('upload fail');
},
});
}
}
request.download
download(Object): void
Downloads files.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
url | string | Yes | Resource URL. |
header | Object | No | Request header. |
description | string | No | Download description. The default value is the file name. |
filename | string | No | Name of the file to download. The value is obtained from the current request or resource URL by default. |
success | Function | No | Called when the download task is complete. |
fail | Function | No | Called when downloading fails or the task does not exist. |
complete | Function | No | Called when the execution is complete. |
Return values of the success callback
Name | Type | Description |
---|---|---|
token | string | Download token, which is used to obtain the download status. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
400 | Download task failed |
Example
export default {
downLoad() {
request.download({
url: 'http://www.path.com',
success: function(data) {
console.log('call success callback success: ' + data.token);
},
fail: function(data, code) {
console.log('handling fail');
},
});
}
}
request.onDownloadComplete
onDownloadComplete(Object): void
Listens to download task status.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
token | string | Yes | Token of the result returned by the download method |
success | Function | No | Called when the download task is complete. |
fail | Function | No | Called when downloading fails or the task does not exist. |
complete | Function | No | Called when the execution is complete. |
Return values of the success callback
Name | Type | Description |
---|---|---|
uri | string | URI of the download file |
One of the following error codes will be returned if the listening fails.
Error Code | Description |
---|---|
400 | Download task failed |
401 | Download task not exist |
Example
export default {
onDownloadComplete() {
request.onDownloadComplete({
token: 'token-index',
success: function(data) {
console.log('download success, uri:' + data.uri);
},
fail: function(data, code) {
console.log('download fail');
},
});
}
}