js-apis-request.md 29.6 KB
Newer Older
Z
zengyawen 已提交
1 2 3
# Upload and Download


E
ester.zhou 已提交
4
> **NOTE**<br>The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
5 6 7 8 9


## Modules to Import


E
ester.zhou 已提交
10
```js
Z
zengyawen 已提交
11 12 13 14 15 16
import request from '@ohos.request';
```


## Constraints

E
ester.zhou 已提交
17 18
HTTPS is supported by default. To support HTTP, you need to add **network** to the **config.json** file and set the **cleartextTraffic** attribute to **true**.

E
ester.zhou 已提交
19 20 21 22 23
```
  "deviceConfig": {
    "default": {
      "network": {
        "cleartextTraffic": true
Z
zengyawen 已提交
24
      }
E
ester.zhou 已提交
25
      ...
Z
zengyawen 已提交
26
    }
E
ester.zhou 已提交
27 28
  }
```
Z
zengyawen 已提交
29 30 31 32


## Constants

E
ester.zhou 已提交
33
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
34 35 36 37 38

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

| Name | Type | Readable | Writable | Description |
| -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| NETWORK_MOBILE | number | Yes | No | Whether download is allowed when the cellular network is used. |
| NETWORK_WIFI | number | Yes | No | Whether download is allowed when the WLAN is used. |
| ERROR_CANNOT_RESUME<sup>7+</sup> | number | Yes | No | Failure to resume the download due to an error. |
| ERROR_DEVICE_NOT_FOUND<sup>7+</sup> | number | Yes | No | Failure to find a storage device such as an SD card. |
| ERROR_FILE_ALREADY_EXISTS<sup>7+</sup> | number | Yes | No | Failure to download the file because it already exists. |
| ERROR_FILE_ERROR<sup>7+</sup> | number | Yes | No | File operation failure. |
| ERROR_HTTP_DATA_ERROR<sup>7+</sup> | number | Yes | No | HTTP transmission failure. |
| ERROR_INSUFFICIENT_SPACE<sup>7+</sup> | number | Yes | No | Insufficient storage space. |
| ERROR_TOO_MANY_REDIRECTS<sup>7+</sup> | number | Yes | No | Error caused by too many network redirections. |
| ERROR_UNHANDLED_HTTP_CODE<sup>7+</sup> | number | Yes | No | Unidentified HTTP code. |
| ERROR_UNKNOWN<sup>7+</sup> | number | Yes | No | Unknown error. |
| PAUSED_QUEUED_FOR_WIFI<sup>7+</sup> | number | Yes | No | Download paused and queuing for WLAN connection, because the file size exceeds the maximum value allowed by a cellular network session. |
| PAUSED_UNKNOWN<sup>7+</sup> | number | Yes | No | Download paused due to unknown reasons. |
| PAUSED_WAITING_FOR_NETWORK<sup>7+</sup> | number | Yes | No | Download paused due to a network connection problem, for example, network disconnection. |
| PAUSED_WAITING_TO_RETRY<sup>7+</sup> | number | Yes | No | Download paused and then retried. |
| SESSION_FAILED<sup>7+</sup> | number | Yes | No | Download failure without retry. |
| SESSION_PAUSED<sup>7+</sup> | number | Yes | No | Download paused. |
| SESSION_PENDING<sup>7+</sup> | number | Yes | No | Download pending. |
| SESSION_RUNNING<sup>7+</sup> | number | Yes | No | Download in progress. |
| SESSION_SUCCESSFUL<sup>7+</sup> | number | Yes | No | Successful download. |
Z
zengyawen 已提交
59 60 61 62 63 64


## request.upload

upload(config: UploadConfig): Promise&lt;UploadTask&gt;

E
ester.zhou 已提交
65
Uploads files. This API uses a promise to return the result.
Z
zengyawen 已提交
66

E
ester.zhou 已提交
67
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
68 69 70

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

E
ester.zhou 已提交
71
**Parameters**
Z
zengyawen 已提交
72

E
ester.zhou 已提交
73 74 75
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| config | [UploadConfig](#uploadconfig) | Yes | Configurations of the upload. |
Z
zengyawen 已提交
76

E
ester.zhou 已提交
77 78 79 80 81 82 83 84 85
**Return value**

| Type | Description |
| -------- | -------- |
| Promise&lt;[UploadTask](#uploadtask)&gt; | Promise used to return the **UploadTask** object. |

**Example**

  ```js
E
ester.zhou 已提交
86
  let file1 = { filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" };
E
ester.zhou 已提交
87
  let data = { name: "name123", value: "123" };
E
ester.zhou 已提交
88
  let header = { key1: "value1", key2: "value2" };
E
ester.zhou 已提交
89
  let uploadTask;
E
ester.zhou 已提交
90
  request.upload({ url: 'https://patch', header: header, method: "POST", files: [file1], data: [data] }).then((data) => {
E
ester.zhou 已提交
91 92 93 94
      uploadTask = data;
  }).catch((err) => {
      console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
  })
Z
zengyawen 已提交
95 96 97 98 99 100 101
  ```


## request.upload

upload(config: UploadConfig, callback: AsyncCallback&lt;UploadTask&gt;): void

E
ester.zhou 已提交
102
Uploads files. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
103

E
ester.zhou 已提交
104
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
105 106 107

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

E
ester.zhou 已提交
108
**Parameters**
Z
zengyawen 已提交
109

E
ester.zhou 已提交
110 111 112 113 114 115 116 117
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| config | [UploadConfig](#uploadconfig) | Yes | Configurations of the upload. |
| callback | AsyncCallback&lt;[UploadTask](#uploadtask)&gt; | No | Callback used to return the **UploadTask** object. |

**Example**

  ```js
E
ester.zhou 已提交
118
  let file1 = { filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" };
E
ester.zhou 已提交
119
  let data = { name: "name123", value: "123" };
E
ester.zhou 已提交
120
  let header = { key1: "value1", key2: "value2" };
E
ester.zhou 已提交
121
  let uploadTask;
E
ester.zhou 已提交
122
  request.upload({ url: 'https://patch', header: header, method: "POST", files: [file1], data: [data] }, (err, data) => {
E
ester.zhou 已提交
123 124 125 126 127 128
      if (err) {
          console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
          return;
      }
      uploadTask = data;
  });
Z
zengyawen 已提交
129 130 131 132 133
  ```


## UploadTask

E
ester.zhou 已提交
134
Implements file uploads. Before using any APIs of this class, you must obtain an **UploadTask** object.
Z
zengyawen 已提交
135 136 137 138 139 140


### on('progress')

on(type: 'progress', callback:(uploadedSize: number, totalSize: number) =&gt; void): void

E
ester.zhou 已提交
141
Subscribes to the upload progress event. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
142

E
ester.zhou 已提交
143
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
144 145 146

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

E
ester.zhou 已提交
147
**Parameters**
Z
zengyawen 已提交
148

E
ester.zhou 已提交
149 150 151 152
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| type | string | Yes | Type of the event to subscribe to. The value is **progress** (upload progress). |
| callback | function | Yes | Callback for the upload progress event. |
Z
zengyawen 已提交
153

E
ester.zhou 已提交
154
Parameters of the callback function
Z
zengyawen 已提交
155

E
ester.zhou 已提交
156 157 158 159 160 161 162 163
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| uploadedSize | number | Yes | Size of the uploaded files, in KB. |
| totalSize | number | Yes | Total size of the files to upload, in KB. |

**Example**

  ```js
E
ester.zhou 已提交
164 165 166 167
  uploadTask.on('progress', function callback(uploadedSize, totalSize) {
      console.info("upload totalSize:" + totalSize + "  uploadedSize:" + uploadedSize);
  }
  );
Z
zengyawen 已提交
168 169 170 171 172 173 174
  ```


### on('headerReceive')<sup>7+</sup>

on(type: 'headerReceive', callback:  (header: object) =&gt; void): void

E
ester.zhou 已提交
175
Subscribes to the **headerReceive** event, which is triggered when an HTTP response header is received. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
176

E
ester.zhou 已提交
177
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
178 179 180

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

E
ester.zhou 已提交
181
**Parameters**
Z
zengyawen 已提交
182

E
ester.zhou 已提交
183 184
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
185
| type | string | Yes | Type of the event to subscribe to. The value is **'headerReceive'** (response header). |
E
ester.zhou 已提交
186
| callback | function | Yes | Callback for the HTTP Response Header event. |
Z
zengyawen 已提交
187

E
ester.zhou 已提交
188
Parameters of the callback function
Z
zengyawen 已提交
189

E
ester.zhou 已提交
190 191 192 193 194 195 196
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| header | object | Yes | HTTP Response Header. |

**Example**

  ```js
E
ester.zhou 已提交
197 198 199 200
  uploadTask.on('headerReceive', function callback(headers){   
      console.info("upOnHeader headers:" + JSON.stringify(headers));
  }
  );
Z
zengyawen 已提交
201 202 203 204 205 206 207
  ```


### off('progress')

off(type:  'progress',  callback?: (uploadedSize: number, totalSize: number) =&gt;  void): void

E
ester.zhou 已提交
208
Unsubscribes from the upload progress event. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
209

E
ester.zhou 已提交
210
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
211 212 213

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

E
ester.zhou 已提交
214
**Parameters**
Z
zengyawen 已提交
215

E
ester.zhou 已提交
216 217
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
218
| type | string | Yes | Type of the event to unsubscribe from. The value is **'progress'** (upload progress). |
E
ester.zhou 已提交
219
| callback | function | No | Callback for the upload progress event. |
Z
zengyawen 已提交
220

E
ester.zhou 已提交
221
Parameters of the callback function
Z
zengyawen 已提交
222

E
ester.zhou 已提交
223 224 225 226 227 228 229 230
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| uploadedSize | number | Yes | Size of the uploaded files, in KB. |
| totalSize | number | Yes | Total size of the files to upload, in KB. |

**Example**

  ```js
E
ester.zhou 已提交
231 232 233 234
  uploadTask.off('progress', function callback(uploadedSize, totalSize) {
      console.info('uploadedSize: ' + uploadedSize, 'totalSize: ' + totalSize);
  }
  );
Z
zengyawen 已提交
235 236 237 238 239 240 241
  ```


### off('headerReceive')<sup>7+</sup>

off(type: 'headerReceive', callback?: (header: object) =&gt; void): void

E
ester.zhou 已提交
242
Unsubscribes from the **headerReceive** event. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
243

E
ester.zhou 已提交
244
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
245 246 247

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

E
ester.zhou 已提交
248
**Parameters**
Z
zengyawen 已提交
249

E
ester.zhou 已提交
250 251
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
252
| type | string | Yes | Type of the event to unsubscribe from. The value is **'headerReceive'** (response header). |
E
ester.zhou 已提交
253
| callback | function | No | Callback for the HTTP Response Header event. |
Z
zengyawen 已提交
254

E
ester.zhou 已提交
255
Parameters of the callback function
Z
zengyawen 已提交
256

E
ester.zhou 已提交
257 258 259 260 261 262 263
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| header | object | Yes | HTTP Response Header. |

**Example**

  ```js
E
ester.zhou 已提交
264 265 266 267
  uploadTask.off('headerReceive', function callback(headers) {
      console.info("upOnHeader headers:" + JSON.stringify(headers));
  }
  );
Z
zengyawen 已提交
268 269 270 271 272 273 274
  ```


### remove

remove(): Promise&lt;boolean&gt;

E
ester.zhou 已提交
275
Removes this upload task. This API uses a promise to return the result.
Z
zengyawen 已提交
276

E
ester.zhou 已提交
277
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
278 279 280

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

E
ester.zhou 已提交
281
**Return value**
Z
zengyawen 已提交
282

E
ester.zhou 已提交
283 284 285 286 287 288 289
| Type | Description |
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return the task removal result. If **true** is returned, the task is removed. If **false** is returned, the task fails to be removed. |

**Example**

  ```js
E
ester.zhou 已提交
290 291 292 293 294 295 296 297 298
  uploadTask.remove().then((result) => {
      if (result) {
          console.info('Upload task removed successfully. ');
      } else {
          console.error('Failed to remove the upload task. ');
      }
  }).catch((err) => {
      console.error('Failed to remove the upload task. Cause: ' + JSON.stringify(err));
  });
Z
zengyawen 已提交
299 300 301 302 303 304 305
  ```


### remove

remove(callback: AsyncCallback&lt;boolean&gt;): void

E
ester.zhou 已提交
306
Removes this upload task. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
307

E
ester.zhou 已提交
308
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
309 310 311

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

E
ester.zhou 已提交
312
**Parameters**
Z
zengyawen 已提交
313

E
ester.zhou 已提交
314 315 316 317 318 319 320
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback used to return the result. |

**Example**

  ```js
E
ester.zhou 已提交
321 322 323 324 325 326 327 328 329 330 331
  uploadTask.remove((err, result) => {
      if (err) {
          console.error('Failed to remove the upload task. Cause: ' + JSON.stringify(err));
          return;
      }
      if (result) {
          console.info('Upload task removed successfully.');
      } else {
          console.error('Failed to remove the upload task.');
      }
  });
Z
zengyawen 已提交
332 333 334 335 336 337 338 339 340
  ```


## UploadConfig

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

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
341
| url | string | Yes | Resource URL. |
E
ester.zhou 已提交
342 343
| header | object | Yes | HTTP or HTTPS header added to an upload request. |
| method | string | Yes | Request methods available: **POST** and **PUT**. The default value is **POST**. |
E
ester.zhou 已提交
344
| files | Array&lt;[File](#file)&gt; | Yes | List of files to upload, which is submitted through **multipart/form-data**. |
E
ester.zhou 已提交
345
| data | Array&lt;[RequestData](#requestdata)&gt; | Yes | Form data in the request body. |
Z
zengyawen 已提交
346 347 348 349 350 351 352 353


## File

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

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
354 355
| 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**. |
E
ester.zhou 已提交
356
| uri | string | Yes | Local path for storing files.<br/>The **dataability** and **internal** protocol types are supported. However, the **internal** protocol type supports only temporary directories. Below are examples:<br>dataability:///com.domainname.dataability.persondata/person/10/file.txt<br>internal://cache/path/to/file.txt |
E
ester.zhou 已提交
357
| type | string | No | Type of the file content. By default, the type is obtained based on the extension of the file name or URI. |
Z
zengyawen 已提交
358 359 360 361 362 363 364 365


## RequestData

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

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
366 367
| name | string | Yes | Name of a form element. |
| value | string | Yes | Value of a form element. |
Z
zengyawen 已提交
368 369 370 371 372 373


## request.download

download(config: DownloadConfig): Promise&lt;DownloadTask&gt;

E
ester.zhou 已提交
374
Downloads files. This API uses a promise to return the result.
Z
zengyawen 已提交
375

E
ester.zhou 已提交
376
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
377 378 379

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

E
ester.zhou 已提交
380
**Parameters**
Z
zengyawen 已提交
381

E
ester.zhou 已提交
382 383 384
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| config | [DownloadConfig](#downloadconfig) | Yes | Configurations of the download. |
Z
zengyawen 已提交
385

E
ester.zhou 已提交
386 387 388 389 390 391 392 393
**Return value**

| Type | Description |
| -------- | -------- |
| Promise&lt;[DownloadTask](#downloadtask)&gt; | Promise used to return the result. |

**Example**

E
ester.zhou 已提交
394 395 396 397 398 399 400 401
  ```js
  let downloadTask;
  request.download({ url: 'https://xxxx/xxxx.hap' }).then((data) => {
      downloadTask = data;
  }).catch((err) => {
      console.error('Failed to request the download. Cause: ' + JSON.stringify(err));
  })
  ```
Z
zengyawen 已提交
402 403 404 405 406 407


## request.download

download(config: DownloadConfig, callback: AsyncCallback&lt;DownloadTask&gt;): void

E
ester.zhou 已提交
408
Downloads files. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
409

E
ester.zhou 已提交
410
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
411 412 413

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

E
ester.zhou 已提交
414
**Parameters**
Z
zengyawen 已提交
415

E
ester.zhou 已提交
416 417 418 419 420 421 422 423
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| config | [DownloadConfig](#downloadconfig) | Yes | Configurations of the download. |
| callback | AsyncCallback&lt;[DownloadTask](#downloadtask)&gt; | No | Callback used to return the result. |

**Example**

  ```js
E
ester.zhou 已提交
424 425 426 427 428 429 430 431 432
  let downloadTask;
  request.download({ url: 'https://xxxx/xxxxx.hap', 
  filePath: 'xxx/xxxxx.hap'}, (err, data) => {
      if (err) {
          console.error('Failed to request the download. Cause: ' + JSON.stringify(err));
          return;
      }
      downloadTask = data;
  });
Z
zengyawen 已提交
433 434 435 436 437 438 439 440 441 442 443 444
  ```


## DownloadTask

Implements file downloads.


### on('progress')

on(type: 'progress', callback:(receivedSize: number, totalSize: number) =&gt; void): void

E
ester.zhou 已提交
445
Subscribes to the download progress event. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
446

E
ester.zhou 已提交
447
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
448 449 450

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

E
ester.zhou 已提交
451 452 453 454
**Parameters**

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
455
| type | string | Yes | Type of the event to subscribe to. The value is **'progress'** (download progress). |
E
ester.zhou 已提交
456
| callback | function | Yes | Callback for the download progress event. |
Z
zengyawen 已提交
457

E
ester.zhou 已提交
458
Parameters of the callback function
Z
zengyawen 已提交
459

E
ester.zhou 已提交
460 461 462 463
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| receivedSize | number | Yes | Size of the downloaded files, in KB. |
| totalSize | number | Yes | Total size of the files to download, in KB. |
Z
zengyawen 已提交
464

E
ester.zhou 已提交
465 466 467
**Example**

  ```js
Z
zengyawen 已提交
468 469 470 471 472 473 474 475 476 477 478 479
      downloadTask.on('progress', function download_callback(receivedSize, totalSize) {
      console.info("download receivedSize:" + receivedSize + " totalSize:" + totalSize);
  }
  );
  });
  ```


### off('progress')

off(type: 'progress', callback?: (receivedSize: number, totalSize: number) =&gt; void): void

E
ester.zhou 已提交
480
Unsubscribes from the download progress event. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
481

E
ester.zhou 已提交
482
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
483 484 485

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

E
ester.zhou 已提交
486
**Parameters**
Z
zengyawen 已提交
487

E
ester.zhou 已提交
488 489
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
490
| type | string | Yes | Type of the event to unsubscribe from. The value is **'progress'** (download progress). |
E
ester.zhou 已提交
491
| callback | function | No | Callback for the download progress event. |
Z
zengyawen 已提交
492

E
ester.zhou 已提交
493
Parameters of the callback function
Z
zengyawen 已提交
494

E
ester.zhou 已提交
495 496 497 498 499 500 501 502
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| receivedSize | number | Yes | Size of the downloaded files, in KB. |
| totalSize | number | Yes | Total size of the files to download, in KB. |

**Example**

  ```js
E
ester.zhou 已提交
503 504 505 506 507
      downloadTask .off('progress', function download_callback(receivedSize, totalSize) {
          console.info("download receivedSize:" + receivedSize + " totalSize:" + totalSize);
      }
  );
  });
Z
zengyawen 已提交
508 509 510 511 512 513 514
  ```


### on('complete'|'pause'|'remove')<sup>7+</sup>

on(type: 'complete'|'pause'|'remove', callback:() =&gt; void): void

E
ester.zhou 已提交
515
Subscribes to a download event. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
516

E
ester.zhou 已提交
517
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
518 519 520

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

E
ester.zhou 已提交
521
**Parameters**
Z
zengyawen 已提交
522

E
ester.zhou 已提交
523 524
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
525
| type | string | Yes | Event type.<br/>- **'complete'**: download task completion event.<br/>- **'pause'**: download task pause event.<br/>- **'remove'**: download task removal event. |
E
ester.zhou 已提交
526 527 528 529 530
| callback | function | Yes | Callback used to return the result. |

**Example**

  ```js
E
ester.zhou 已提交
531 532 533 534 535
      downloadTask.on('complete', function callback() {
          console.info('Download task completed.');
      }
  );
  });
Z
zengyawen 已提交
536 537 538 539 540 541 542
  ```


### off('complete'|'pause'|'remove')<sup>7+</sup>

off(type: 'complete'|'pause'|'remove', callback?:() =&gt; void): void

E
ester.zhou 已提交
543
Unsubscribes from the download event. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
544

E
ester.zhou 已提交
545
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
546 547 548

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

E
ester.zhou 已提交
549
**Parameters**
Z
zengyawen 已提交
550

E
ester.zhou 已提交
551 552
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
553
| type | string | Yes | Event type.<br/>- **'complete'**: download task completion event.<br/>- **'pause'**: download task pause event.<br/>- **'remove'**: download task removal event. |
E
ester.zhou 已提交
554 555 556 557 558
| callback | function | No | Callback used to return the result. |

**Example**

  ```js
E
ester.zhou 已提交
559 560 561 562 563
      downloadTask.off('complete', function callback() {
          console.info('Download task completed.');
      }
  );
  }); 
Z
zengyawen 已提交
564 565 566 567 568 569 570
  ```


### on('fail')<sup>7+</sup>

on(type: 'fail', callback: (err: number) =&gt; void): void

E
ester.zhou 已提交
571
Subscribes to the download task failure event. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
572

E
ester.zhou 已提交
573
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
574 575 576

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

E
ester.zhou 已提交
577
**Parameters**
Z
zengyawen 已提交
578

E
ester.zhou 已提交
579 580
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
581
| type | string | Yes | Type of the subscribed event. The value is **'fail'** (download failure). |
E
ester.zhou 已提交
582
| callback | function | Yes | Callback for the download task failure event. |
Z
zengyawen 已提交
583

E
ester.zhou 已提交
584
Parameters of the callback function
Z
zengyawen 已提交
585

E
ester.zhou 已提交
586 587 588 589 590 591
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| err | number | Yes | Error code of the download failure. For details about the error cause, see [ERROR_*](#constants). |

**Example**

E
ester.zhou 已提交
592 593 594 595 596 597
  ```js 
      downloadTask.on('fail', function callBack(err) {
          console.info('Download task failed. Cause:' + err);
      }
  );
  });
Z
zengyawen 已提交
598 599 600 601 602 603 604
  ```


### off('fail')<sup>7+</sup>

off(type: 'fail', callback?: (err: number) =&gt; void): void

E
ester.zhou 已提交
605
Unsubscribes from the download task failure event. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
606

E
ester.zhou 已提交
607
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
608 609 610

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

E
ester.zhou 已提交
611 612 613 614
**Parameters**

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
615
| type | string | Yes | Type of the event to unsubscribe from. The value is **'fail'** (download failure). |
E
ester.zhou 已提交
616
| callback | function | No | Callback for the download task failure event. |
Z
zengyawen 已提交
617

E
ester.zhou 已提交
618
Parameters of the callback function
Z
zengyawen 已提交
619

E
ester.zhou 已提交
620 621 622
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| err | number | Yes | Error code of the download failure. For details about the error cause, see [ERROR_*](#constants). |
Z
zengyawen 已提交
623

E
ester.zhou 已提交
624 625 626
**Example**

  ```js
Z
zengyawen 已提交
627 628 629 630 631 632 633 634 635 636 637 638
      downloadTask.off('fail', function callBack(err) {
          console.info('Download task failed. Cause:' + err);
      } 
  );
  });
  ```


### remove

remove(): Promise&lt;boolean&gt;

E
ester.zhou 已提交
639
Removes this download task. This API uses a promise to return the result.
Z
zengyawen 已提交
640

E
ester.zhou 已提交
641
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
642 643 644

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

E
ester.zhou 已提交
645
**Return value**
Z
zengyawen 已提交
646

E
ester.zhou 已提交
647 648 649 650 651 652 653
| Type | Description |
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return the task removal result. |

**Example**

  ```js
Z
zengyawen 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
  downloadTask.remove().then((result) => {
      if (result) {
          console.info('Download task removed.');
      } else {
          console.error('Failed to remove the download task.');
      }
  }).catch ((err) => {
      console.error('Failed to remove the download task.');
  });
  ```


### remove

remove(callback: AsyncCallback&lt;boolean&gt;): void

E
ester.zhou 已提交
670
Removes this download task. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
671

E
ester.zhou 已提交
672
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
673 674 675

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

E
ester.zhou 已提交
676
**Parameters**
Z
zengyawen 已提交
677

E
ester.zhou 已提交
678 679 680 681 682 683 684
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback used to return the task removal result. |

**Example**

  ```js
E
ester.zhou 已提交
685 686 687 688 689 690 691 692 693 694 695
  downloadTask.remove((err, result)=>{
      if(err) {
          console.error('Failed to remove the download task.');
          return;
      } 
      if (result) {
          console.info('Download task removed.');
      } else {
          console.error('Failed to remove the download task.');
      } 
  });
Z
zengyawen 已提交
696 697 698 699 700 701 702
  ```


### query<sup>7+</sup>

query(): Promise&lt;DownloadInfo&gt;

E
ester.zhou 已提交
703
Queries this download task. This API uses a promise to return the result.
Z
zengyawen 已提交
704

E
ester.zhou 已提交
705
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
706 707 708

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

E
ester.zhou 已提交
709
**Parameters**
Z
zengyawen 已提交
710

E
ester.zhou 已提交
711 712 713 714 715 716 717
| Type | Description |
| -------- | -------- |
| Promise&lt;[DownloadInfo](#downloadinfo7)&gt; | Promise used to return the download task information. |

**Example**

  ```js
E
ester.zhou 已提交
718 719 720 721 722
  downloadTask.query().then((downloadInfo) => {    
      console.info('Download task queried. Data:' + JSON.stringify(downloadInfo))
  }) .catch((err) => {
      console.error('Failed to query the download task. Cause:' + err)
  });
Z
zengyawen 已提交
723 724 725 726 727 728 729
  ```


### query<sup>7+</sup>

query(callback: AsyncCallback&lt;DownloadInfo&gt;): void

E
ester.zhou 已提交
730
Queries this download task. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
731

E
ester.zhou 已提交
732
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
733 734 735

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

E
ester.zhou 已提交
736
**Parameters**
Z
zengyawen 已提交
737

E
ester.zhou 已提交
738 739 740 741 742 743 744
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;[DownloadInfo](#downloadinfo7)&gt; | Yes | Callback used to return the download task information. |

**Example**

  ```js
E
ester.zhou 已提交
745 746 747 748 749 750 751
  downloadTask.query((err, downloadInfo)=>{
      if(err) {
          console.error('Failed to query the download mimeType. Cause:' + JSON.stringify(err));
      } else {
          console.info('download query success. data:'+ JSON.stringify(downloadInfo));
      }
  });
Z
zengyawen 已提交
752 753 754 755 756 757 758
  ```


### queryMimeType<sup>7+</sup>

queryMimeType(): Promise&lt;string&gt;

E
ester.zhou 已提交
759
Queries **MimeType** of this download task. This API uses a promise to return the result.
Z
zengyawen 已提交
760

E
ester.zhou 已提交
761
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
762 763 764

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

E
ester.zhou 已提交
765
**Return value**
Z
zengyawen 已提交
766

E
ester.zhou 已提交
767 768 769 770 771 772 773
| Type | Description |
| -------- | -------- |
| Promise&lt;string&gt; | Promise used to return **MimeType** of the download task. |

**Example**

  ```js
E
ester.zhou 已提交
774 775 776 777 778
  downloadTask.queryMimeType().then((data) => {    
      console.info('Download task queried. Data:' + JSON.stringify(data));
  }).catch((err) => {
      console.error('Failed to query the download MimeType. Cause:' + JSON.stringify(err))
  });
Z
zengyawen 已提交
779 780 781 782 783 784 785
  ```


### queryMimeType<sup>7+</sup>

queryMimeType(callback: AsyncCallback&lt;string&gt;): void;

E
ester.zhou 已提交
786
Queries **MimeType** of this download task. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
787

E
ester.zhou 已提交
788
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
789 790 791

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

E
ester.zhou 已提交
792
**Parameters**
Z
zengyawen 已提交
793

E
ester.zhou 已提交
794 795 796 797 798 799 800
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return **MimeType** of the download task. |

**Example**

  ```js
E
ester.zhou 已提交
801 802 803 804 805 806 807
  downloadTask.queryMimeType((err, data)=>{
      if(err) {
          console.error('Failed to query the download mimeType. Cause:' + JSON.stringify(err));
      } else {
          console.info('Download task queried. data:' + JSON.stringify(data));
      }
  });
Z
zengyawen 已提交
808 809 810 811 812 813 814
  ```


### pause<sup>7+</sup>

pause(): Promise&lt;void&gt;

E
ester.zhou 已提交
815
Pauses this download task. This API uses a promise to return the result.
Z
zengyawen 已提交
816

E
ester.zhou 已提交
817
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
818 819 820

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

E
ester.zhou 已提交
821
**Return value**
Z
zengyawen 已提交
822

E
ester.zhou 已提交
823 824 825 826 827 828 829
| Type | Description |
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the download task pause result. |

**Example**

  ```js
E
ester.zhou 已提交
830 831 832 833 834 835 836 837 838
  downloadTask.pause().then((result) => {    
      if (result) {
           console.info('Download task paused. ');
      } else {
          console.error('Failed to pause the download task. Cause:' + JSON.stringify(result));
      }
  }).catch((err) => {
      console.error('Failed to pause the download task. Cause:' + JSON.stringify(err));
  });
Z
zengyawen 已提交
839 840 841 842 843 844 845
  ```


### pause<sup>7+</sup>

pause(callback: AsyncCallback&lt;void&gt;): void

E
ester.zhou 已提交
846
Pauses this download task. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
847

E
ester.zhou 已提交
848
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
849 850 851

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

E
ester.zhou 已提交
852
**Parameters**
Z
zengyawen 已提交
853

E
ester.zhou 已提交
854 855 856 857 858 859 860
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result. |

**Example**

  ```js
E
ester.zhou 已提交
861 862 863 864 865 866 867 868 869 870 871
  downloadTask.pause((err, result)=>{
      if(err) {
          console.error('Failed to pause the download task. Cause:' + JSON.stringify(err));
          return;
      }
      if (result) {
           console.info('Download task paused. ');
      } else {
          console.error('Failed to pause the download task. Cause:' + JSON.stringify(result));
      }
  });
Z
zengyawen 已提交
872 873 874 875 876 877 878
  ```


### resume<sup>7+</sup>

resume(): Promise&lt;void&gt;

E
ester.zhou 已提交
879
Resumes this download task. This API uses a promise to return the result.
Z
zengyawen 已提交
880

E
ester.zhou 已提交
881
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
882 883 884

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

E
ester.zhou 已提交
885
**Parameters**
Z
zengyawen 已提交
886

E
ester.zhou 已提交
887 888 889 890 891 892 893
| Type | Description |
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result. |

**Example**

  ```js
E
ester.zhou 已提交
894 895 896 897 898 899 900 901 902 903
  downloadTask.resume().then((result) => {
      if (result) {
          console.info('Download task resumed.')
      } else {
          console.error('Failed to resume the download task. ');
      }
      console.info('Download task resumed.')
  }).catch((err) => {
      console.error('Failed to resume the download task. Cause:' + err);
  });
Z
zengyawen 已提交
904 905 906 907 908 909 910
  ```


### resume<sup>7+</sup>

resume(callback: AsyncCallback&lt;void&gt;): void

E
ester.zhou 已提交
911
Resumes this download task. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
912

E
ester.zhou 已提交
913
**Required permissions**: ohos.permission.INTERNET
Z
zengyawen 已提交
914 915 916

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

E
ester.zhou 已提交
917
**Parameters**
Z
zengyawen 已提交
918

E
ester.zhou 已提交
919 920 921 922 923 924 925
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result. |

**Example**

  ```js
E
ester.zhou 已提交
926 927 928 929 930 931 932 933 934 935 936
  downloadTask.resume((err, result)=>{
      if (err) {
          console.error('Failed to resume the download task. Cause:' + err);
          return;
      } 
      if (result) {
          console.info('Download task resumed.');
      } else {
          console.error('Failed to resume the download task.');
      }
  });
Z
zengyawen 已提交
937 938 939 940 941 942 943 944 945
  ```


## DownloadConfig

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

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
946 947 948 949 950
| url | string | Yes | Resource URL. |
| header | object | No | HTTP or HTTPS header added to a download request. |
| enableMetered | boolean | No | Download allowed in metered connections. |
| enableRoaming | boolean | No | Download allowed on a roaming network. |
| description | string | No | Description of the download session. |
E
ester.zhou 已提交
951
| filePath<sup>7+</sup> | string | No | Download path. (The default path is **'internal://cache/'**.<br/>- filePath:'workspace/test.txt': The **workspace** directory is created in the default path to store files.<br/>- filePath:'test.txt': Files are stored in the default path.<br/>- filePath:'workspace/': The **workspace** directory is created in the default path to store files. |
E
ester.zhou 已提交
952 953
| networkType | number | No | Network type allowed for download. |
| title | string | No | Title of the download session. |
Z
zengyawen 已提交
954 955 956 957 958 959 960 961


## DownloadInfo<sup>7+</sup>

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

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
962 963 964 965 966 967 968 969 970 971 972
| downloadId | number | Yes | ID of the downloaded file. |
| failedReason | number | No | Download failure cause, which can be any constant of [ERROR_*](#constants). |
| fileName | string | Yes | Name of the downloaded file. |
| filePath | string | Yes | URI of the saved file. |
| pausedReason | number | No | Reason for session pause, which can be any constant of [PAUSED_*](#constants). |
| status | number | Yes | Download status code, which can be any constant of [SESSION_*](#constants). |
| targetURI | string | Yes | URI of the downloaded file. |
| downloadTitle | string | Yes | Title of the downloaded file. |
| downloadTotalBytes | number | Yes | Total size of the downloaded file (int bytes). |
| description | string | Yes | Description of the file to download. |
| downloadedBytes | number | Yes | Size of the files downloaded (int bytes). |