js-apis-system-request.md 8.3 KB
Newer Older
S
shawn_he 已提交
1
# @system.request (Upload and Download)
Z
zengyawen 已提交
2

E
esterzhou 已提交
3 4 5
The **system.request** module provides applications with basic upload and download capabilities.

> **NOTE**
E
ester.zhou 已提交
6
> - The APIs of this module are deprecated since API version 9. You are advised to use [@ohos.request](js-apis-request.md) instead.
Z
zengyawen 已提交
7
> 
E
esterzhou 已提交
8
> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
9 10 11 12 13 14 15 16 17


## Modules to Import


```
import request from '@system.request';
```

E
esterzhou 已提交
18
## request.upload
Z
zengyawen 已提交
19

E
esterzhou 已提交
20
upload(options: UploadRequestOptions): void
Z
zengyawen 已提交
21

E
esterzhou 已提交
22
Uploads a file. This API returns no value.
Z
zengyawen 已提交
23

E
esterzhou 已提交
24
**System capability**: SystemCapability.MiscServices.Upload
Z
zengyawen 已提交
25

E
esterzhou 已提交
26
**Parameters**
Z
zengyawen 已提交
27

E
ester.zhou 已提交
28 29 30
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| options | [UploadRequestOptions](#uploadrequestoptions) | Yes| Upload configurations.|
Z
zengyawen 已提交
31

E
esterzhou 已提交
32
**Example**
Z
zengyawen 已提交
33

E
esterzhou 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  ```js
  let uploadRequestOptions = {
    url: 'http://www.path.com',
    method: 'POST',
    files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }],
    data: [{ name: "name123", value: "123" }],
    success: function(data) {
      console.info(' upload success, code:' + JSON.stringify(data));
    },
    fail: function(data, code) {
      console.info(' upload fail data: ' + data + 'code: ' + code);
    },
    complete: function (){
      console.info(' upload complete');
    }
  }
  try {
    request.upload(uploadRequestOptions);
    console.info('upload start ');
  } catch(err) {
    console.info(' upload err:' + err);
  }
  ```
Z
zengyawen 已提交
57 58


E
esterzhou 已提交
59
## UploadRequestOptions
Z
zengyawen 已提交
60

E
esterzhou 已提交
61
**System capability**: SystemCapability.MiscServices.Upload
Z
zengyawen 已提交
62 63


E
ester.zhou 已提交
64 65 66 67 68 69 70 71 72 73
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| url | string | Yes| URL of the upload server.|
| data | Array<[RequestData](#requestdata)> | No| Form data in the request body.|
| files | Array<[RequestFile](#requestfile)> | Yes| List of files to upload, which is submitted through **multipart/form-data**.|
| header | Object | No| Request header.|
| method | string | No| Request method, which can be **'POST'** or **'PUT'**. The default value is **POST**.|
| success | Function | No| Called when API call is successful.|
| fail | Function | No| Called when API call has failed.|
| complete | Function | No| Called when API call is complete.|
Z
zengyawen 已提交
74

E
esterzhou 已提交
75
**success parameter**
E
ester.zhou 已提交
76 77 78
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| data | [UploadResponse](#uploadresponse) | Yes| Information returned when the upload task is successful.|
Z
zengyawen 已提交
79

E
esterzhou 已提交
80
**fail parameters**
E
ester.zhou 已提交
81 82 83 84
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| data | any | Yes| Header information returned when the upload task fails.|
| code | number | Yes| HTTP status code returned when the upload task fails.|
E
add doc  
ester.zhou 已提交
85

Z
zengyawen 已提交
86 87


E
esterzhou 已提交
88
## UploadResponse
Z
zengyawen 已提交
89

E
esterzhou 已提交
90
**System capability**: SystemCapability.MiscServices.Upload
Z
zengyawen 已提交
91

E
ester.zhou 已提交
92 93 94 95 96
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| code | number | Yes| HTTP status code returned by the server.|
| data | string | Yes| Content returned by the server. The value type is determined by the type in the returned headers.|
| headers | Object | Yes| Headers returned by the server.|
Z
zengyawen 已提交
97 98


E
esterzhou 已提交
99 100 101 102
## RequestFile

**System capability**: SystemCapability.MiscServices.Upload

E
ester.zhou 已提交
103 104 105 106 107 108
| 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 path for storing files.|
| type | string | No| Type of the file content. By default, the type is obtained based on the extension of the file name or URI.|
E
esterzhou 已提交
109 110 111 112 113


## RequestData

**System capability**: SystemCapability.MiscServices.Upload
Z
zengyawen 已提交
114

E
ester.zhou 已提交
115 116 117 118
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the form element.|
| value | string | Yes| Value of the form element.|
Z
zengyawen 已提交
119 120 121



E
esterzhou 已提交
122 123 124 125 126 127 128 129 130
## request.download

download(options: DownloadRequestOptions): void

Downloads a file. This API returns no value.

**System capability**: SystemCapability.MiscServices.Download

**Parameters**
Z
zengyawen 已提交
131

E
ester.zhou 已提交
132 133 134
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| options | [DownloadRequestOptions](#downloadrequestoptions) | Yes| Download configurations.|
Z
zengyawen 已提交
135 136 137

**Example**

E
esterzhou 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  ```js
  let downloadRequestOptions = {
    url: 'http://www.path.com',
    filename: 'requestSystenTest',
    header: '',
    description: 'this is requeSystem download response',
    success: function(data) {
      console.info(' download success, code:' + JSON.stringify(data));
    },
    fail: function(data, code) {
      console.info(' download fail data: ' + data + 'code: ' + code);
    },
    complete: function (){
      console.info(' download complete');
    }
Z
zengyawen 已提交
153
  }
E
esterzhou 已提交
154 155 156 157 158 159 160
  try {
    request.download(downloadRequestOptions);
    console.info('download start ');
  } catch(err) {
    console.info(' download err:' + err);
  }
  ```
Z
zengyawen 已提交
161 162


E
esterzhou 已提交
163
## DownloadRequestOptions
Z
zengyawen 已提交
164

E
esterzhou 已提交
165
**System capability**: SystemCapability.MiscServices.Download
Z
zengyawen 已提交
166

E
ester.zhou 已提交
167 168 169 170 171 172 173 174 175
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| url | string | Yes| Resource URL.|
| filename | string | No| Name of the file to download. The value is obtained from the current request or resource URL by default.|
| header | Object | No| Request header.|
| description | string | No| Download description. The default value is the file name.|
| success | Function | No| Called when API call is successful.|
| fail | Function | No| Called when API call has failed.|
| complete | Function | No| Called when API call is complete.|
Z
zengyawen 已提交
176

E
esterzhou 已提交
177
**success parameter**
E
ester.zhou 已提交
178 179 180
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| data | [DownloadResponse](#downloadresponse) | Yes| Information returned when the download task is successful.|
E
esterzhou 已提交
181 182

**fail parameters**
E
ester.zhou 已提交
183 184 185 186
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| data | any | Yes| Header information returned when the download task fails.|
| code | number | Yes| HTTP status code returned when the download task fails.|
E
esterzhou 已提交
187 188 189 190 191

## DownloadResponse

**System capability**: SystemCapability.MiscServices.Download

E
ester.zhou 已提交
192 193 194
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| token | string | Yes| Download token, which is used to obtain the download status|
E
esterzhou 已提交
195 196 197


## request.onDownloadComplete
Z
zengyawen 已提交
198

E
esterzhou 已提交
199
onDownloadComplete(options: OnDownloadCompleteOptions): void
Z
zengyawen 已提交
200

E
esterzhou 已提交
201
Listens for download task status. This API returns no value.
Z
zengyawen 已提交
202

E
esterzhou 已提交
203
**System capability**: SystemCapability.MiscServices.Download
Z
zengyawen 已提交
204

E
esterzhou 已提交
205
**Parameters**
Z
zengyawen 已提交
206

E
ester.zhou 已提交
207 208 209
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| options | [OnDownloadCompleteOptions](#ondownloadcompleteoptions) | Yes| Configurations of the download task.|
Z
zengyawen 已提交
210 211 212

**Example**

E
esterzhou 已提交
213 214 215 216 217 218 219 220 221 222 223 224
  ```js
  let onDownloadCompleteOptions = {
    token: 'token-index',
    success: function(data) {
      console.info(' download success, code:' + JSON.stringify(data));
    },
    fail: function(data, code) {
      console.info(' download fail data: ' + data + 'code: ' + code);
    },
    complete: function (){
      console.info(' download complete');
    }
Z
zengyawen 已提交
225
  }
E
esterzhou 已提交
226 227 228 229 230 231 232 233
  request.onDownloadComplete(onDownloadCompleteOptions);
  ```


## OnDownloadCompleteOptions

**System capability**: SystemCapability.MiscServices.Download

E
ester.zhou 已提交
234 235 236 237 238 239
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| token | string | Yes| Result token returned by the download API.|
| success | Function | No| Called when API call is successful.|
| fail | Function | No| Called when API call has failed.|
| complete | Function | No| Called when API call is complete.|
E
esterzhou 已提交
240 241

**success parameter**
E
ester.zhou 已提交
242 243 244
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| data | [OnDownloadCompleteResponse](#ondownloadcompleteresponse) | Yes| Information returned when the download task is successful.|
E
esterzhou 已提交
245 246

**fail parameters**
E
ester.zhou 已提交
247 248 249 250
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| data | any | Yes| Header information returned when the download task fails.|
| code | number | Yes| HTTP status code returned when the download task fails.|
E
esterzhou 已提交
251 252 253 254 255 256


## OnDownloadCompleteResponse

**System capability**: SystemCapability.MiscServices.Download

E
ester.zhou 已提交
257 258 259
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| uri | string | Yes| URI of the download file.|