1# Uploading and Downloading 2 3> **NOTE** 4> - The APIs of this module are no longer maintained since API version 6. It is recommended that you use [`@ohos.request`](js-apis-request.md) instead. 5> 6> - 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. 7 8 9## Modules to Import 10 11 12``` 13import request from '@system.request'; 14``` 15 16## Required Permissions 17 18ohos.permission.INTERNET. 19 20 21## request.upload 22 23upload(Object): void 24 25Uploads files. 26 27**Parameters** 28 29| Name | Type | Mandatory | Description | 30| -------- | -------- | -------- | -------- | 31| url | string | Yes | URL of the upload server. | 32| header | Object | No | Request header. | 33| method | string | No | Request methods available: **POST** and **PUT**. The default value is **POST**. | 34| files | Array<File> | Yes | List of files to upload, which is submitted through **multipart/form-data**. | 35| data | Array<RequestData> | No | Form data in the request body. | 36| success | Function | No | Called when the download task is complete. | 37| fail | Function | No | Called when downloading fails or the task does not exist. | 38| complete | Function | No | Called when the execution is complete. | 39 40**Table 1** File 41 42| Name | Type | Mandatory | Description | 43| -------- | -------- | -------- | -------- | 44| filename | string | No | File name in the header when **multipart** is used. | 45| name | string | No | Name of a form item when **multipart** is used. The default value is **file**. | 46| uri | string | Yes | Local storage path of a file. | 47| type | string | No | Type of the file content. By default, the type is obtained based on the suffix of the file name or URI. | 48 49**Table 2** RequestData 50 51| Name | Type | Mandatory | Description | 52| -------- | -------- | -------- | -------- | 53| name | string | Yes | Name of the form element | 54| value | string | Yes | Value of the form element | 55 56When the files are successfully uploaded, the following values will be returned. 57 58| Name | Type | Description | 59| -------- | -------- | -------- | 60| code | number | HTTP status code returned by the server. | 61| data | string | Content returned by the server. The value type is determined by the type in the returned headers. | 62| headers | Object | Headers returned by the server. | 63 64When the files fail to be uploaded, an HTTP status code is returned in **code** of **data**. 65 66**Example** 67 68``` 69export default { 70 upLoad() { 71 request.upload({ 72 url: 'http://www.path.com', 73 files: [ 74 { 75 uri: 'internal://cache/path/to/file.txt', 76 name: 'file', 77 filename: 'file.txt', 78 }, 79 ], 80 data:[ 81 { 82 name: 'name1', 83 value: 'value', 84 }, 85 ], 86 success: function(data) { 87 console.log('upload success, code:' + data.code); 88 }, 89 fail: function() { 90 console.log('upload fail'); 91 }, 92 }); 93 } 94} 95``` 96 97 98## request.download 99 100download(Object): void 101 102Downloads files. 103 104**Parameters** 105 106| Name | Type | Mandatory | Description | 107| -------- | -------- | -------- | -------- | 108| url | string | Yes | Resource URL. | 109| header | Object | No | Request header. | 110| description | string | No | Download description. The default value is the file name. | 111| filename | string | No | Name of the file to download. The value is obtained from the current request or resource URL by default. | 112| success | Function | No | Called when the download task is complete. | 113| fail | Function | No | Called when downloading fails or the task does not exist. | 114| complete | Function | No | Called when the execution is complete. | 115 116Return values of the **success** callback 117 118| Name | Type | Description | 119| -------- | -------- | -------- | 120| token | string | Download token, which is used to obtain the download status. | 121 122One of the following error codes will be returned if the operation fails. 123 124| Error Code | Description | 125| -------- | -------- | 126| 400 | Download task failed | 127 128**Example** 129 130``` 131export default { 132 downLoad() { 133 request.download({ 134 url: 'http://www.path.com', 135 success: function(data) { 136 console.log('call success callback success: ' + data.token); 137 }, 138 fail: function(data, code) { 139 console.log('handling fail'); 140 }, 141 }); 142 } 143} 144``` 145 146 147## request.onDownloadComplete 148 149onDownloadComplete(Object): void 150 151Listens to download task status. 152 153**Parameters** 154 155| Name | Type | Mandatory | Description | 156| -------- | -------- | -------- | -------- | 157| token | string | Yes | Token of the result returned by the download method | 158| success | Function | No | Called when the download task is complete. | 159| fail | Function | No | Called when downloading fails or the task does not exist. | 160| complete | Function | No | Called when the execution is complete. | 161 162Return values of the **success** callback 163 164| Name | Type | Description | 165| -------- | -------- | -------- | 166| uri | string | URI of the download file | 167 168One of the following error codes will be returned if the listening fails. 169 170| Error Code | Description | 171| -------- | -------- | 172| 400 | Download task failed | 173| 401 | Download task not exist | 174 175**Example** 176 177``` 178export default { 179 onDownloadComplete() { 180 request.onDownloadComplete({ 181 token: 'token-index', 182 success: function(data) { 183 console.log('download success, uri:' + data.uri); 184 }, 185 fail: function(data, code) { 186 console.log('download fail'); 187 }, 188 }); 189 } 190} 191```