1# @ohos.request (Upload and Download) 2 3The **request** module provides applications with basic upload, download, and background transmission agent capabilities. 4 5> **NOTE** 6> 7> 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. 8 9 10## Modules to Import 11 12 13```js 14import request from '@ohos.request'; 15``` 16 17## Constants 18 19**Required permissions**: ohos.permission.INTERNET 20 21**System capability**: SystemCapability.MiscServices.Download 22 23### Network Types 24You can set **networkType** in [DownloadConfig](#downloadconfig) to specify the network type for the download service. 25 26| Name| Type| Value| Description| 27| -------- | -------- | -------- | -------- | 28| NETWORK_MOBILE | number | 0x00000001 | Whether download is allowed on a mobile network.| 29| NETWORK_WIFI | number | 0x00010000 | Whether download is allowed on a WLAN.| 30 31### Download Error Codes 32The table below lists the values of **err** in the callback of [on('fail')<sup>7+</sup>](#onfail7) and the values of **failedReason** returned by [getTaskInfo<sup>9+</sup>](#gettaskinfo9). 33 34| Name| Type| Value| Description| 35| -------- | -------- | -------- | -------- | 36| ERROR_CANNOT_RESUME<sup>7+</sup> | number | 0 | Failure to resume the download due to network errors.| 37| ERROR_DEVICE_NOT_FOUND<sup>7+</sup> | number | 1 | Failure to find a storage device such as a memory card.| 38| ERROR_FILE_ALREADY_EXISTS<sup>7+</sup> | number | 2 | Failure to download the file because it already exists.| 39| ERROR_FILE_ERROR<sup>7+</sup> | number | 3 | File operation failure.| 40| ERROR_HTTP_DATA_ERROR<sup>7+</sup> | number | 4 | HTTP transmission failure.| 41| ERROR_INSUFFICIENT_SPACE<sup>7+</sup> | number | 5 | Insufficient storage space.| 42| ERROR_TOO_MANY_REDIRECTS<sup>7+</sup> | number | 6 | Error caused by too many network redirections.| 43| ERROR_UNHANDLED_HTTP_CODE<sup>7+</sup> | number | 7 | Unidentified HTTP code.| 44| ERROR_UNKNOWN<sup>7+</sup> | number | 8 | Unknown error.| 45| ERROR_OFFLINE<sup>9+</sup> | number | 9 | No network connection.| 46| ERROR_UNSUPPORTED_NETWORK_TYPE<sup>9+</sup> | number | 10 | Network type mismatch.| 47 48 49### Causes of Download Pause 50The table below lists the values of **pausedReason** returned by [getTaskInfo<sup>9+</sup>](#gettaskinfo9). 51 52| Name| Type| Value| Description| 53| -------- | -------- | -------- | -------- | 54| PAUSED_QUEUED_FOR_WIFI<sup>7+</sup> | number | 0 | Download paused and queuing for a WLAN connection, because the file size exceeds the maximum value allowed by a mobile network session.| 55| PAUSED_WAITING_FOR_NETWORK<sup>7+</sup> | number | 1 | Download paused due to a network connection problem, for example, network disconnection.| 56| PAUSED_WAITING_TO_RETRY<sup>7+</sup> | number | 2 | Download paused and then retried.| 57| PAUSED_BY_USER<sup>9+</sup> | number | 3 | The user paused the session.| 58| PAUSED_UNKNOWN<sup>7+</sup> | number | 4 | Download paused due to unknown reasons.| 59 60### Download Task Status Codes 61The table below lists the values of **status** returned by [getTaskInfo<sup>9+</sup>](#gettaskinfo9). 62 63| Name| Type| Value| Description| 64| -------- | -------- | -------- | -------- | 65| SESSION_SUCCESSFUL<sup>7+</sup> | number | 0 | Successful download.| 66| SESSION_RUNNING<sup>7+</sup> | number | 1 | Download in progress.| 67| SESSION_PENDING<sup>7+</sup> | number | 2 | Download pending.| 68| SESSION_PAUSED<sup>7+</sup> | number | 3 | Download paused.| 69| SESSION_FAILED<sup>7+</sup> | number | 4 | Download failure without retry.| 70 71 72## request.uploadFile<sup>9+</sup> 73 74uploadFile(context: BaseContext, config: UploadConfig): Promise<UploadTask> 75 76Uploads files. This API uses a promise to return the result. You can use [on('complete'|'fail')<sup>9+</sup>](#oncomplete--fail9) to obtain the upload error information. 77 78**Required permissions**: ohos.permission.INTERNET 79 80**System capability**: SystemCapability.MiscServices.Upload 81 82**Parameters** 83 84 | Name| Type| Mandatory| Description| 85 | -------- | -------- | -------- | -------- | 86 | context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.| 87 | config | [UploadConfig](#uploadconfig) | Yes| Upload configurations.| 88 89 90**Return value** 91 92 | Type| Description| 93 | -------- | -------- | 94 | Promise<[UploadTask](#uploadtask)> | Promise used to return the **UploadTask** object.| 95 96**Error codes** 97 98For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 99 100 | ID| Error Message| 101 | -------- | -------- | 102 | 13400002 | bad file path. | 103 104**Example** 105 106 ```js 107 let uploadTask; 108 let context; 109 let uploadConfig = { 110 url: 'http://patch', 111 header: { key1: "value1", key2: "value2" }, 112 method: "POST", 113 files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }], 114 data: [{ name: "name123", value: "123" }], 115 }; 116 try { 117 request.uploadFile(context, uploadConfig).then((data) => { 118 uploadTask = data; 119 }).catch((err) => { 120 console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`); 121 }); 122 } catch (err) { 123 console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`); 124 } 125 ``` 126 127> **NOTE** 128> 129> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 130 131 132## request.uploadFile<sup>9+</sup> 133 134uploadFile(context: BaseContext, config: UploadConfig, callback: AsyncCallback<UploadTask>): void 135 136Uploads files. This API uses an asynchronous callback to return the result. You can use [on('complete'|'fail')<sup>9+</sup>](#oncomplete--fail9) to obtain the upload error information. 137 138**Required permissions**: ohos.permission.INTERNET 139 140**System capability**: SystemCapability.MiscServices.Upload 141 142**Parameters** 143 144 | Name| Type| Mandatory| Description| 145 | -------- | -------- | -------- | -------- | 146 | context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.| 147 | config | [UploadConfig](#uploadconfig) | Yes| Upload configurations.| 148 | callback | AsyncCallback<[UploadTask](#uploadtask)> | Yes| Callback used to return the **UploadTask** object.| 149 150**Error codes** 151 152For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 153 154 | ID| Error Message| 155 | -------- | -------- | 156 | 13400002 | bad file path. | 157 158**Example** 159 160 ```js 161 let uploadTask; 162 let context; 163 let uploadConfig = { 164 url: 'http://patch', 165 header: { key1: "value1", key2: "value2" }, 166 method: "POST", 167 files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }], 168 data: [{ name: "name123", value: "123" }], 169 }; 170 try { 171 request.uploadFile(context, uploadConfig, (err, data) => { 172 if (err) { 173 console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`); 174 return; 175 } 176 uploadTask = data; 177 }); 178 } catch (err) { 179 console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`); 180 } 181 ``` 182 183> **NOTE** 184> 185> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 186 187## request.upload<sup>(deprecated)</sup> 188 189upload(config: UploadConfig): Promise<UploadTask> 190 191Uploads files. This API uses a promise to return the result. 192 193**Model restriction**: This API can be used only in the FA model. 194 195> **NOTE** 196> 197> This API is deprecated since API version 9. You are advised to use [request.uploadFile<sup>9+</sup>](#requestuploadfile9). 198 199**Required permissions**: ohos.permission.INTERNET 200 201**System capability**: SystemCapability.MiscServices.Upload 202 203**Parameters** 204 205 | Name| Type| Mandatory| Description| 206 | -------- | -------- | -------- | -------- | 207 | config | [UploadConfig](#uploadconfig) | Yes| Upload configurations.| 208 209**Return value** 210 211 | Type| Description| 212 | -------- | -------- | 213 | Promise<[UploadTask](#uploadtask)> | Promise used to return the **UploadTask** object.| 214 215**Example** 216 217 ```js 218 let uploadTask; 219 let uploadConfig = { 220 url: 'http://patch', 221 header: { key1: "value1", key2: "value2" }, 222 method: "POST", 223 files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }], 224 data: [{ name: "name123", value: "123" }], 225 }; 226 request.upload(uploadConfig).then((data) => { 227 uploadTask = data; 228 }).catch((err) => { 229 console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`); 230 }) 231 ``` 232 233 234## request.upload<sup>(deprecated)</sup> 235 236upload(config: UploadConfig, callback: AsyncCallback<UploadTask>): void 237 238Uploads files. This API uses an asynchronous callback to return the result. 239 240**Model restriction**: This API can be used only in the FA model. 241 242> **NOTE** 243> 244> This API is deprecated since API version 9. You are advised to use [request.uploadFile<sup>9+</sup>](#requestuploadfile9-1). 245 246**Required permissions**: ohos.permission.INTERNET 247 248**System capability**: SystemCapability.MiscServices.Upload 249 250**Parameters** 251 252 | Name| Type| Mandatory| Description| 253 | -------- | -------- | -------- | -------- | 254 | config | [UploadConfig](#uploadconfig) | Yes| Upload configurations.| 255 | callback | AsyncCallback<[UploadTask](#uploadtask)> | Yes| Callback used to return the **UploadTask** object.| 256 257**Example** 258 259 ```js 260 let uploadTask; 261 let uploadConfig = { 262 url: 'http://patch', 263 header: { key1: "value1", key2: "value2" }, 264 method: "POST", 265 files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }], 266 data: [{ name: "name123", value: "123" }], 267 }; 268 request.upload(uploadConfig, (err, data) => { 269 if (err) { 270 console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`); 271 return; 272 } 273 uploadTask = data; 274 }); 275 ``` 276 277## UploadTask 278 279Implements file uploads. Before using any APIs of this class, you must obtain an **UploadTask** object through [request.uploadFile<sup>9+</sup>](#requestuploadfile9) in promise mode or [request.uploadFile<sup>9+</sup>](#requestuploadfile9-1) in callback mode. 280 281 282 283### on('progress') 284 285on(type: 'progress', callback:(uploadedSize: number, totalSize: number) => void): void 286 287Subscribes to upload progress events. This API uses a callback to return the result synchronously. 288 289> **NOTE** 290> 291> To maintain a balance between power consumption and performance, this API cannot be called when the application is running in the background. 292 293**Required permissions**: ohos.permission.INTERNET 294 295**System capability**: SystemCapability.MiscServices.Upload 296 297**Parameters** 298 299 | Name| Type| Mandatory| Description| 300 | -------- | -------- | -------- | -------- | 301 | type | string | Yes| Type of the event to subscribe to. The value is **'progress'** (upload progress).| 302 | callback | function | Yes| Callback for the upload progress event.| 303 304 Parameters of the callback function 305 306| Name| Type| Mandatory| Description| 307| -------- | -------- | -------- | -------- | 308| uploadedSize | number | Yes| Size of the uploaded files, in bytes.| 309| totalSize | number | Yes| Total size of the files to upload, in bytes.| 310 311**Example** 312 313 ```js 314 let upProgressCallback = (uploadedSize, totalSize) => { 315 console.info("upload totalSize:" + totalSize + " uploadedSize:" + uploadedSize); 316 }; 317 uploadTask.on('progress', upProgressCallback); 318 ``` 319 320 321### on('headerReceive')<sup>7+</sup> 322 323on(type: 'headerReceive', callback: (header: object) => void): void 324 325Subscribes to HTTP header events for an upload task. This API uses a callback to return the result synchronously. 326 327**Required permissions**: ohos.permission.INTERNET 328 329**System capability**: SystemCapability.MiscServices.Upload 330 331**Parameters** 332 333 | Name| Type| Mandatory| Description| 334 | -------- | -------- | -------- | -------- | 335 | type | string | Yes| Type of the event to subscribe to. The value is **'headerReceive'** (response header).| 336 | callback | function | Yes| Callback for the HTTP Response Header event.| 337 338 Parameters of the callback function 339 340| Name| Type| Mandatory| Description| 341| -------- | -------- | -------- | -------- | 342| header | object | Yes| HTTP Response Header.| 343 344**Example** 345 346 ```js 347 let headerCallback = (headers) => { 348 console.info("upOnHeader headers:" + JSON.stringify(headers)); 349 }; 350 uploadTask.on('headerReceive', headerCallback); 351 ``` 352 353 354### on('complete' | 'fail')<sup>9+</sup> 355 356 on(type:'complete' | 'fail', callback: Callback<Array<TaskState>>): void; 357 358Subscribes to upload completion or failure events. This API uses a callback to return the result synchronously. 359 360**Required permissions**: ohos.permission.INTERNET 361 362**System capability**: SystemCapability.MiscServices.Upload 363 364**Parameters** 365 366 | Name| Type| Mandatory| Description| 367 | -------- | -------- | -------- | -------- | 368 | type | string | Yes| Type of the event to subscribe to. The value **'complete'** means the upload completion event, and **'fail'** means the upload failure event.| 369 | callback | Callback<Array<TaskState>> | Yes| Callback used to return the result.| 370 371 Parameters of the callback function 372 373| Name| Type| Mandatory| Description| 374| -------- | -------- | -------- | -------- | 375| taskstates | Array<[TaskState](#taskstate9)> | Yes| Upload result.| 376 377**Example** 378 379 ```js 380 let upCompleteCallback = (taskStates) => { 381 for (let i = 0; i < taskStates.length; i++ ) { 382 console.info("upOnComplete taskState:" + JSON.stringify(taskStates[i])); 383 } 384 }; 385 uploadTask.on('complete', upCompleteCallback); 386 387 let upFailCallback = (taskStates) => { 388 for (let i = 0; i < taskStates.length; i++ ) { 389 console.info("upOnFail taskState:" + JSON.stringify(taskStates[i])); 390 } 391 }; 392 uploadTask.on('fail', upFailCallback); 393 ``` 394 395 396### off('progress') 397 398off(type: 'progress', callback?: (uploadedSize: number, totalSize: number) => void): void 399 400Unsubscribes from upload progress events. This API uses a callback to return the result synchronously. 401 402**Required permissions**: ohos.permission.INTERNET 403 404**System capability**: SystemCapability.MiscServices.Upload 405 406**Parameters** 407 408 | Name| Type| Mandatory| Description| 409 | -------- | -------- | -------- | -------- | 410 | type | string | Yes| Type of the event to unsubscribe from. The value is **'progress'** (upload progress).| 411 | callback | function | No| Callback used to return the result.<br>**uploadedSize**: size of the uploaded files, in B.<br>**totalSize**: Total size of the files to upload, in B.| 412 413**Example** 414 415 ```js 416 let upProgressCallback = (uploadedSize, totalSize) => { 417 console.info('Upload delete progress notification.' + 'totalSize:' + totalSize + 'uploadedSize:' + uploadedSize); 418 }; 419 uploadTask.off('progress', upProgressCallback); 420 ``` 421 422 423### off('headerReceive')<sup>7+</sup> 424 425off(type: 'headerReceive', callback?: (header: object) => void): void 426 427Unsubscribes from HTTP header events for an upload task. This API uses a callback to return the result synchronously. 428 429**Required permissions**: ohos.permission.INTERNET 430 431**System capability**: SystemCapability.MiscServices.Upload 432 433**Parameters** 434 435 | Name| Type| Mandatory| Description| 436 | -------- | -------- | -------- | -------- | 437 | type | string | Yes| Type of the event to unsubscribe from. The value is **'headerReceive'** (response header).| 438 | callback | function | No| Callback used to return the result.<br>**header**: HTTP response header.| 439 440**Example** 441 442 ```js 443 let headerCallback = (header) => { 444 console.info(`Upload delete headerReceive notification. header: ${JSON.stringify(header)}`); 445 }; 446 uploadTask.off('headerReceive', headerCallback); 447 ``` 448 449### off('complete' | 'fail')<sup>9+</sup> 450 451 off(type:'complete' | 'fail', callback?: Callback<Array<TaskState>>): void; 452 453Unsubscribes from upload completion or failure events. This API uses a callback to return the result synchronously. 454 455**Required permissions**: ohos.permission.INTERNET 456 457**System capability**: SystemCapability.MiscServices.Upload 458 459**Parameters** 460 461 | Name| Type| Mandatory| Description| 462 | -------- | -------- | -------- | -------- | 463 | type | string | Yes| Type of the event to subscribe to. The value **'complete'** means the upload completion event, and **'fail'** means the upload failure event.| 464 | callback | Callback<Array<TaskState>> | No| Callback used to return the result.<br>**taskstates**: upload task result.| 465 466**Example** 467 468 ```js 469 let upCompleteCallback = (taskStates) => { 470 console.info('Upload delete complete notification.'); 471 for (let i = 0; i < taskStates.length; i++ ) { 472 console.info('taskState:' + JSON.stringify(taskStates[i])); 473 } 474 }; 475 uploadTask.off('complete', upCompleteCallback); 476 477 let upFailCallback = (taskStates) => { 478 console.info('Upload delete fail notification.'); 479 for (let i = 0; i < taskStates.length; i++ ) { 480 console.info('taskState:' + JSON.stringify(taskStates[i])); 481 } 482 }; 483 uploadTask.off('fail', upFailCallback); 484 ``` 485 486### delete<sup>9+</sup> 487delete(): Promise<boolean> 488 489Deletes this upload task. This API uses a promise to return the result. 490 491**Required permissions**: ohos.permission.INTERNET 492 493**System capability**: SystemCapability.MiscServices.Upload 494 495**Return value** 496 497 | Type| Description| 498 | -------- | -------- | 499 | Promise<boolean> | Promise used to return the task removal result. It returns **true** if the operation is successful and returns **false** otherwise.| 500 501**Example** 502 503 ```js 504 uploadTask.delete().then((result) => { 505 console.info('Succeeded in deleting the upload task.'); 506 }).catch((err) => { 507 console.error(`Failed to delete the upload task. Code: ${err.code}, message: ${err.message}`); 508 }); 509 ``` 510 511 512### delete<sup>9+</sup> 513 514delete(callback: AsyncCallback<boolean>): void 515 516Deletes this upload task. This API uses an asynchronous callback to return the result. 517 518**Required permissions**: ohos.permission.INTERNET 519 520**System capability**: SystemCapability.MiscServices.Upload 521 522**Parameters** 523 524 | Name| Type| Mandatory| Description| 525 | -------- | -------- | -------- | -------- | 526 | callback | AsyncCallback<boolean> | Yes| Callback used to return the result.| 527 528**Example** 529 530 ```js 531 uploadTask.delete((err, result) => { 532 if (err) { 533 console.error(`Failed to delete the upload task. Code: ${err.code}, message: ${err.message}`); 534 return; 535 } 536 console.info('Succeeded in deleting the upload task.'); 537 }); 538 ``` 539 540 541### remove<sup>(deprecated)</sup> 542 543remove(): Promise<boolean> 544 545Removes this upload task. This API uses a promise to return the result. 546 547> **NOTE** 548> 549> This API is deprecated since API version 9. You are advised to use [delete<sup>9+</sup>](#delete9). 550 551**Required permissions**: ohos.permission.INTERNET 552 553**System capability**: SystemCapability.MiscServices.Upload 554 555**Return value** 556 557 | Type| Description| 558 | -------- | -------- | 559 | Promise<boolean> | Promise used to return the task removal result. It returns **true** if the operation is successful and returns **false** otherwise.| 560 561**Example** 562 563 ```js 564 uploadTask.remove().then((result) => { 565 console.info('Succeeded in removing the upload task.'); 566 }).catch((err) => { 567 console.error(`Failed to remove the upload task. Code: ${err.code}, message: ${err.message}`); 568 }); 569 ``` 570 571 572### remove<sup>(deprecated)</sup> 573 574remove(callback: AsyncCallback<boolean>): void 575 576Removes this upload task. This API uses an asynchronous callback to return the result. 577 578> **NOTE** 579> 580> This API is deprecated since API version 9. You are advised to use [delete<sup>9+</sup>](#delete9-1). 581 582**Required permissions**: ohos.permission.INTERNET 583 584**System capability**: SystemCapability.MiscServices.Upload 585 586**Parameters** 587 588 | Name| Type| Mandatory| Description| 589 | -------- | -------- | -------- | -------- | 590 | callback | AsyncCallback<boolean> | Yes| Callback used to return the result.| 591 592**Example** 593 594 ```js 595 uploadTask.remove((err, result) => { 596 if (err) { 597 console.error(`Failed to remove the upload task. Code: ${err.code}, message: ${err.message}`); 598 return; 599 } 600 if (result) { 601 console.info('Succeeded in removing the upload task.'); 602 } else { 603 console.error(`Failed to remove the upload task. Code: ${err.code}, message: ${err.message}`); 604 } 605 }); 606 ``` 607 608## UploadConfig 609Describes the configuration for an upload task. 610 611**Required permissions**: ohos.permission.INTERNET 612 613**System capability**: SystemCapability.MiscServices.Upload 614 615| Name| Type| Mandatory| Description| 616| -------- | -------- | -------- | -------- | 617| url | string | Yes| Resource URL.| 618| header | Object | Yes| HTTP or HTTPS header added to an upload request.| 619| method | string | Yes| Request method, which can be **'POST'** or **'PUT'**. The default value is **'POST'**.| 620| files | Array<[File](#file)> | Yes| List of files to upload, which is submitted through **multipart/form-data**.| 621| data | Array<[RequestData](#requestdata)> | Yes| Form data in the request body.| 622 623## TaskState<sup>9+</sup> 624Implements a **TaskState** object, which is the callback parameter of the [on('complete' | 'fail')<sup>9+</sup>](#oncomplete--fail9) and [off('complete' | 'fail')<sup>9+</sup>](#offcomplete--fail9) APIs. 625 626**Required permissions**: ohos.permission.INTERNET 627 628**System capability**: SystemCapability.MiscServices.Upload 629 630| Name| Type| Mandatory| Description| 631| -------- | -------- | -------- | -------- | 632| path | string | Yes| File path.| 633| responseCode | number | Yes| Return value of an upload task.| 634| message | string | Yes| Description of the upload task result.| 635 636## File 637Describes the list of files in [UploadConfig](#uploadconfig). 638 639**Required permissions**: ohos.permission.INTERNET 640 641**System capability**: SystemCapability.MiscServices.Download 642 643| Name| Type| Mandatory| Description| 644| -------- | -------- | -------- | -------- | 645| filename | string | Yes| File name in the header when **multipart** is used.| 646| name | string | Yes| Name of a form item when **multipart** is used. The default value is **file**.| 647| uri | string | Yes| Local path for storing files.<br>Only the **internal** protocol type is supported. In the value, **"internal://cache/"** must be included. Example:<br>internal://cache/path/to/file.txt | 648| type | string | Yes| Type of the file content. By default, the type is obtained based on the extension of the file name or URI.| 649 650 651## RequestData 652Describes the form data in [UploadConfig](#uploadconfig). 653 654**Required permissions**: ohos.permission.INTERNET 655 656**System capability**: SystemCapability.MiscServices.Download 657 658| Name| Type| Mandatory| Description| 659| -------- | -------- | -------- | -------- | 660| name | string | Yes| Name of a form element.| 661| value | string | Yes| Value of a form element.| 662 663## request.downloadFile<sup>9+</sup> 664 665downloadFile(context: BaseContext, config: DownloadConfig): Promise<DownloadTask> 666 667Downloads files. This API uses a promise to return the result. You can use [on('complete'|'pause'|'remove')<sup>7+</sup>](#oncompletepauseremove7) to obtain the download task state, which can be completed, paused, or removed. You can also use [on('fail')<sup>7+</sup>](#onfail7) to obtain the task download error information. 668 669 670**Required permissions**: ohos.permission.INTERNET 671 672**System capability**: SystemCapability.MiscServices.Download 673 674**Parameters** 675 676 | Name| Type| Mandatory| Description| 677 | -------- | -------- | -------- | -------- | 678 | context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.| 679 | config | [DownloadConfig](#downloadconfig) | Yes| Download configurations.| 680 681**Return value** 682 683 | Type| Description| 684 | -------- | -------- | 685 | Promise<[DownloadTask](#downloadtask)> | Promise used to return the result.| 686 687**Error codes** 688 689For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 690 691 | ID| Error Message| 692 | -------- | -------- | 693 | 13400001 | file operation error. | 694 | 13400002 | bad file path. | 695 | 13400003 | task service ability error. | 696 697**Example** 698 699 ```js 700 let downloadTask; 701 let context; 702 try { 703 request.downloadFile(context, { url: 'https://xxxx/xxxx.hap' }).then((data) => { 704 downloadTask = data; 705 }).catch((err) => { 706 console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`); 707 }) 708 } catch (err) { 709 console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`); 710 } 711 ``` 712 713> **NOTE** 714> 715> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 716 717 718## request.downloadFile<sup>9+</sup> 719 720downloadFile(context: BaseContext, config: DownloadConfig, callback: AsyncCallback<DownloadTask>): void; 721 722Downloads files. This API uses an asynchronous callback to return the result. You can use [on('complete'|'pause'|'remove')<sup>7+</sup>](#oncompletepauseremove7) to obtain the download task state, which can be completed, paused, or removed. You can also use [on('fail')<sup>7+</sup>](#onfail7) to obtain the task download error information. 723 724 725**Required permissions**: ohos.permission.INTERNET 726 727**System capability**: SystemCapability.MiscServices.Download 728 729**Parameters** 730 731 | Name| Type| Mandatory| Description| 732 | -------- | -------- | -------- | -------- | 733 | context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.| 734 | config | [DownloadConfig](#downloadconfig) | Yes| Download configurations.| 735 | callback | AsyncCallback<[DownloadTask](#downloadtask)> | Yes| Callback used to return the result.| 736 737**Error codes** 738 739For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 740 741 | ID| Error Message| 742 | -------- | -------- | 743 | 13400001 | file operation error. | 744 | 13400002 | bad file path. | 745 | 13400003 | task service ability error. | 746 747**Example** 748 749 ```js 750 let downloadTask; 751 let context; 752 try { 753 request.downloadFile(context, { url: 'https://xxxx/xxxxx.hap', 754 filePath: 'xxx/xxxxx.hap'}, (err, data) => { 755 if (err) { 756 console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`); 757 return; 758 } 759 downloadTask = data; 760 }); 761 } catch (err) { 762 console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`); 763 } 764 ``` 765 766> **NOTE** 767> 768> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 769 770## request.download<sup>(deprecated)</sup> 771 772download(config: DownloadConfig): Promise<DownloadTask> 773 774Downloads files. This API uses a promise to return the result. 775 776> **NOTE** 777> 778> This API is deprecated since API version 9. You are advised to use [request.downloadFile<sup>9+</sup>](#requestdownloadfile9). 779 780**Model restriction**: This API can be used only in the FA model. 781 782**Required permissions**: ohos.permission.INTERNET 783 784**System capability**: SystemCapability.MiscServices.Download 785 786**Parameters** 787 788 | Name| Type| Mandatory| Description| 789 | -------- | -------- | -------- | -------- | 790 | config | [DownloadConfig](#downloadconfig) | Yes| Download configurations.| 791 792**Return value** 793 794 | Type| Description| 795 | -------- | -------- | 796 | Promise<[DownloadTask](#downloadtask)> | Promise used to return the result.| 797 798**Example** 799 800 ```js 801 let downloadTask; 802 request.download({ url: 'https://xxxx/xxxx.hap' }).then((data) => { 803 downloadTask = data; 804 }).catch((err) => { 805 console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`); 806 }) 807 ``` 808 809 810## request.download<sup>(deprecated)</sup> 811 812download(config: DownloadConfig, callback: AsyncCallback<DownloadTask>): void 813 814Downloads files. This API uses an asynchronous callback to return the result. 815 816> **NOTE** 817> 818> This API is deprecated since API version 9. You are advised to use [request.downloadFile<sup>9+</sup>](#requestdownloadfile9-1). 819 820**Model restriction**: This API can be used only in the FA model. 821 822**Required permissions**: ohos.permission.INTERNET 823 824**System capability**: SystemCapability.MiscServices.Download 825 826**Parameters** 827 828 | Name| Type| Mandatory| Description| 829 | -------- | -------- | -------- | -------- | 830 | config | [DownloadConfig](#downloadconfig) | Yes| Download configurations.| 831 | callback | AsyncCallback<[DownloadTask](#downloadtask)> | Yes| Callback used to return the result.| 832 833**Example** 834 835 ```js 836 let downloadTask; 837 request.download({ url: 'https://xxxx/xxxxx.hap', 838 filePath: 'xxx/xxxxx.hap'}, (err, data) => { 839 if (err) { 840 console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`); 841 return; 842 } 843 downloadTask = data; 844 }); 845 ``` 846 847## DownloadTask 848 849Implements file downloads. Before using any APIs of this class, you must obtain a **DownloadTask** object through [request.downloadFile<sup>9+</sup>](#requestdownloadfile9) in promise mode or [request.downloadFile<sup>9+</sup>](#requestdownloadfile9-1) in callback mode. 850 851 852### on('progress') 853 854on(type: 'progress', callback:(receivedSize: number, totalSize: number) => void): void 855 856Subscribes to download progress events. This API uses a callback to return the result synchronously. 857 858> **NOTE** 859> 860> To maintain a balance between power consumption and performance, this API cannot be called when the application is running in the background. 861 862**Required permissions**: ohos.permission.INTERNET 863 864**System capability**: SystemCapability.MiscServices.Download 865 866**Parameters** 867 868 | Name| Type| Mandatory| Description| 869 | -------- | -------- | -------- | -------- | 870 | type | string | Yes| Type of the event to subscribe to. The value is **'progress'** (download progress).| 871 | callback | function | Yes| Callback used to return the result.| 872 873 Parameters of the callback function 874 875| Name| Type| Mandatory| Description| 876| -------- | -------- | -------- | -------- | 877| receivedSize | number | Yes| Size of the downloaded files, in bytes.| 878| totalSize | number | Yes| Total size of the files to download, in bytes.| 879 880**Example** 881 882 ```js 883 let progresCallback = (receivedSize, totalSize) => { 884 console.info("download receivedSize:" + receivedSize + " totalSize:" + totalSize); 885 }; 886 downloadTask.on('progress', progresCallback); 887 ``` 888 889 890### off('progress') 891 892off(type: 'progress', callback?: (receivedSize: number, totalSize: number) => void): void 893 894Unsubscribes from download progress events. This API uses a callback to return the result synchronously. 895 896**Required permissions**: ohos.permission.INTERNET 897 898**System capability**: SystemCapability.MiscServices.Download 899 900**Parameters** 901 902 | Name| Type| Mandatory| Description| 903 | -------- | -------- | -------- | -------- | 904 | type | string | Yes| Type of the event to unsubscribe from. The value is **'progress'** (download progress).| 905 | callback | function | No| Callback used to return the result.<br>**receivedSize**: size of the downloaded files.<br>**totalSize**: total size of the files to download.| 906 907**Example** 908 909 ```js 910 let progresCallback = (receivedSize, totalSize) => { 911 console.info('Download delete progress notification.' + 'receivedSize:' + receivedSize + 'totalSize:' + totalSize); 912 }; 913 downloadTask.off('progress', progresCallback); 914 ``` 915 916 917### on('complete'|'pause'|'remove')<sup>7+</sup> 918 919on(type: 'complete'|'pause'|'remove', callback:() => void): void 920 921Subscribes to download events. This API uses an asynchronous callback to return the result. 922 923**Required permissions**: ohos.permission.INTERNET 924 925**System capability**: SystemCapability.MiscServices.Download 926 927**Parameters** 928 929 | Name| Type| Mandatory| Description| 930 | -------- | -------- | -------- | -------- | 931 | type | string | Yes| Type of the event to subscribe to.<br>- **'complete'**: download task completion event.<br>- **'pause'**: download task pause event.<br>- **'remove'**: download task removal event.| 932 | callback | function | Yes| Callback used to return the result.| 933 934**Example** 935 936 ```js 937 let completeCallback = () => { 938 console.info('Download task completed.'); 939 }; 940 downloadTask.on('complete', completeCallback); 941 942 let pauseCallback = () => { 943 console.info('Download task pause.'); 944 }; 945 downloadTask.on('pause', pauseCallback); 946 947 let removeCallback = () => { 948 console.info('Download task remove.'); 949 }; 950 downloadTask.on('remove', removeCallback); 951 ``` 952 953 954### off('complete'|'pause'|'remove')<sup>7+</sup> 955 956off(type: 'complete'|'pause'|'remove', callback?:() => void): void 957 958Unsubscribes from download events. This API uses a callback to return the result synchronously. 959 960**Required permissions**: ohos.permission.INTERNET 961 962**System capability**: SystemCapability.MiscServices.Download 963 964**Parameters** 965 966 | Name| Type| Mandatory| Description| 967 | -------- | -------- | -------- | -------- | 968 | type | string | Yes| Type of the event to unsubscribe from.<br>- **'complete'**: download task completion event.<br>- **'pause'**: download task pause event.<br>- **'remove'**: download task removal event.| 969 | callback | function | No| Callback used to return the result.| 970 971**Example** 972 973 ```js 974 let completeCallback = () => { 975 console.info('Download delete complete notification.'); 976 }; 977 downloadTask.off('complete', completeCallback); 978 979 let pauseCallback = () => { 980 console.info('Download delete pause notification.'); 981 }; 982 downloadTask.off('pause', pauseCallback); 983 984 let removeCallback = () => { 985 console.info('Download delete remove notification.'); 986 }; 987 downloadTask.off('remove', removeCallback); 988 ``` 989 990 991### on('fail')<sup>7+</sup> 992 993on(type: 'fail', callback: (err: number) => void): void 994 995Subscribes to download failure events. This API uses a callback to return the result synchronously. 996 997**Required permissions**: ohos.permission.INTERNET 998 999**System capability**: SystemCapability.MiscServices.Download 1000 1001**Parameters** 1002 1003 | Name| Type| Mandatory| Description| 1004 | -------- | -------- | -------- | -------- | 1005 | type | string | Yes| Type of the event to subscribe to. The value is **'fail'** (download failure).| 1006 | callback | function | Yes| Callback for the download task failure event.| 1007 1008 Parameters of the callback function 1009 1010| Name| Type| Mandatory| Description| 1011| -------- | -------- | -------- | -------- | 1012| err | number | Yes| Error code of the download failure. For details about the error codes, see [Download Error Codes](#download-error-codes).| 1013 1014**Example** 1015 1016 ```js 1017 let failCallback = (err) => { 1018 console.error(`Failed to download the task. Code: ${err.code}, message: ${err.message}`); 1019 }; 1020 downloadTask.on('fail', failCallback); 1021 ``` 1022 1023 1024### off('fail')<sup>7+</sup> 1025 1026off(type: 'fail', callback?: (err: number) => void): void 1027 1028Unsubscribes from download failure events. This API uses a callback to return the result synchronously. 1029 1030**Required permissions**: ohos.permission.INTERNET 1031 1032**System capability**: SystemCapability.MiscServices.Download 1033 1034**Parameters** 1035 1036 | Name| Type| Mandatory| Description| 1037 | -------- | -------- | -------- | -------- | 1038 | type | string | Yes| Type of the event to unsubscribe from. The value is **'fail'** (download failure).| 1039 | callback | function | No| Callback used to return the result.<br>**err**: error code of the download failure. | 1040 1041**Example** 1042 1043 ```js 1044 let failCallback = (err) => { 1045 console.error(`Failed to download the task. Code: ${err.code}, message: ${err.message}`); 1046 }; 1047 downloadTask.off('fail', failCallback); 1048 ``` 1049 1050### delete<sup>9+</sup> 1051 1052delete(): Promise<boolean> 1053 1054Removes this download task. This API uses a promise to return the result. 1055 1056**Required permissions**: ohos.permission.INTERNET 1057 1058**System capability**: SystemCapability.MiscServices.Download 1059 1060**Return value** 1061 1062 | Type| Description| 1063 | -------- | -------- | 1064 | Promise<boolean> | Promise used to return the task removal result.| 1065 1066**Example** 1067 1068 ```js 1069 downloadTask.delete().then((result) => { 1070 console.info('Succeeded in removing the download task.'); 1071 }).catch ((err) => { 1072 console.error(`Failed to remove the download task. Code: ${err.code}, message: ${err.message}`); 1073 }); 1074 ``` 1075 1076 1077### delete<sup>9+</sup> 1078 1079delete(callback: AsyncCallback<boolean>): void 1080 1081Deletes this download task. This API uses an asynchronous callback to return the result. 1082 1083**Required permissions**: ohos.permission.INTERNET 1084 1085**System capability**: SystemCapability.MiscServices.Download 1086 1087**Parameters** 1088 1089 | Name| Type| Mandatory| Description| 1090 | -------- | -------- | -------- | -------- | 1091 | callback | AsyncCallback<boolean> | Yes| Callback used to return the task deletion result.| 1092 1093**Example** 1094 1095 ```js 1096 downloadTask.delete((err, result)=>{ 1097 if(err) { 1098 console.error(`Failed to remove the download task. Code: ${err.code}, message: ${err.message}`); 1099 return; 1100 } 1101 console.info('Succeeded in removing the download task.'); 1102 }); 1103 ``` 1104 1105 1106### getTaskInfo<sup>9+</sup> 1107 1108getTaskInfo(): Promise<DownloadInfo> 1109 1110Obtains the information about this download task. This API uses a promise to return the result. 1111 1112**Required permissions**: ohos.permission.INTERNET 1113 1114**System capability**: SystemCapability.MiscServices.Download 1115 1116**Return value** 1117 1118 | Type| Description| 1119 | -------- | -------- | 1120 | Promise<[DownloadInfo](#downloadinfo7)> | Promise used to return the download task information.| 1121 1122**Example** 1123 1124 ```js 1125 downloadTask.getTaskInfo().then((downloadInfo) => { 1126 console.info('Succeeded in querying the download task') 1127 }) .catch((err) => { 1128 console.error(`Failed to query the download task. Code: ${err.code}, message: ${err.message}`) 1129 }); 1130 ``` 1131 1132 1133### getTaskInfo<sup>9+</sup> 1134 1135getTaskInfo(callback: AsyncCallback<DownloadInfo>): void 1136 1137Obtains the information about this download task. This API uses an asynchronous callback to return the result. 1138 1139**Required permissions**: ohos.permission.INTERNET 1140 1141**System capability**: SystemCapability.MiscServices.Download 1142 1143**Parameters** 1144 1145 | Name| Type| Mandatory| Description| 1146 | -------- | -------- | -------- | -------- | 1147 | callback | AsyncCallback<[DownloadInfo](#downloadinfo7)> | Yes| Callback used to return the download task information.| 1148 1149**Example** 1150 1151 ```js 1152 downloadTask.getTaskInfo((err, downloadInfo)=>{ 1153 if(err) { 1154 console.error(`Failed to query the download mimeType. Code: ${err.code}, message: ${err.message}`); 1155 } else { 1156 console.info('Succeeded in querying the download mimeType'); 1157 } 1158 }); 1159 ``` 1160 1161 1162### getTaskMimeType<sup>9+</sup> 1163 1164getTaskMimeType(): Promise<string> 1165 1166Obtains the **MimeType** of this download task. This API uses a promise to return the result. 1167 1168**Required permissions**: ohos.permission.INTERNET 1169 1170**System capability**: SystemCapability.MiscServices.Download 1171 1172**Return value** 1173 1174 | Type| Description| 1175 | -------- | -------- | 1176 | Promise<string> | Promise used to return the **MimeType** of the download task.| 1177 1178**Example** 1179 1180 ```js 1181 downloadTask.getTaskMimeType().then((data) => { 1182 console.info('Succeeded in querying the download MimeType'); 1183 }).catch((err) => { 1184 console.error(`Failed to query the download MimeType. Code: ${err.code}, message: ${err.message}`) 1185 }); 1186 ``` 1187 1188 1189### getTaskMimeType<sup>9+</sup> 1190 1191getTaskMimeType(callback: AsyncCallback<string>): void; 1192 1193Obtains the **MimeType** of this download task. This API uses an asynchronous callback to return the result. 1194 1195**Required permissions**: ohos.permission.INTERNET 1196 1197**System capability**: SystemCapability.MiscServices.Download 1198 1199**Parameters** 1200 1201 | Name| Type| Mandatory| Description| 1202 | -------- | -------- | -------- | -------- | 1203 | callback | AsyncCallback<string> | Yes| Callback used to return the **MimeType** of the download task.| 1204 1205**Example** 1206 1207 ```js 1208 downloadTask.getTaskMimeType((err, data)=>{ 1209 if(err) { 1210 console.error(`Failed to query the download mimeType. Code: ${err.code}, message: ${err.message}`); 1211 } else { 1212 console.info('Succeeded in querying the download mimeType'); 1213 } 1214 }); 1215 ``` 1216 1217 1218### suspend<sup>9+</sup> 1219 1220suspend(): Promise<boolean> 1221 1222Pauses this download task. This API uses a promise to return the result. 1223 1224**Required permissions**: ohos.permission.INTERNET 1225 1226**System capability**: SystemCapability.MiscServices.Download 1227 1228**Return value** 1229 1230 | Type| Description| 1231 | -------- | -------- | 1232 | Promise<boolean> | Promise used to return the download task pause result.| 1233 1234**Example** 1235 1236 ```js 1237 downloadTask.suspend().then((result) => { 1238 console.info('Succeeded in pausing the download task.'); 1239 }).catch((err) => { 1240 console.error(`Failed to pause the download task. Code: ${err.code}, message: ${err.message}`); 1241 }); 1242 ``` 1243 1244 1245### suspend<sup>9+</sup> 1246 1247suspend(callback: AsyncCallback<boolean>): void 1248 1249Pauses this download task. This API uses an asynchronous callback to return the result. 1250 1251**Required permissions**: ohos.permission.INTERNET 1252 1253**System capability**: SystemCapability.MiscServices.Download 1254 1255**Parameters** 1256 1257 | Name| Type| Mandatory| Description| 1258 | -------- | -------- | -------- | -------- | 1259 | callback | AsyncCallback<boolean> | Yes| Callback used to return the result.| 1260 1261**Example** 1262 1263 ```js 1264 downloadTask.suspend((err, result)=>{ 1265 if(err) { 1266 console.error(`Failed to pause the download task. Code: ${err.code}, message: ${err.message}`); 1267 return; 1268 } 1269 console.info('Succeeded in pausing the download task.'); 1270 }); 1271 ``` 1272 1273 1274### restore<sup>9+</sup> 1275 1276restore(): Promise<boolean> 1277 1278Resumes this download task. This API uses a promise to return the result. 1279 1280**Required permissions**: ohos.permission.INTERNET 1281 1282**System capability**: SystemCapability.MiscServices.Download 1283 1284**Return value** 1285 1286 | Type| Description| 1287 | -------- | -------- | 1288 | Promise<boolean> | Promise used to return the result.| 1289 1290**Example** 1291 1292 ```js 1293 downloadTask.restore().then((result) => { 1294 console.info('Succeeded in resuming the download task.') 1295 }).catch((err) => { 1296 console.error(`Failed to resume the download task. Code: ${err.code}, message: ${err.message}`); 1297 }); 1298 ``` 1299 1300 1301### restore<sup>9+</sup> 1302 1303restore(callback: AsyncCallback<boolean>): void 1304 1305Resumes this download task. This API uses an asynchronous callback to return the result. 1306 1307**Required permissions**: ohos.permission.INTERNET 1308 1309**System capability**: SystemCapability.MiscServices.Download 1310 1311**Parameters** 1312 1313 | Name| Type| Mandatory| Description| 1314 | -------- | -------- | -------- | -------- | 1315 | callback | AsyncCallback<boolean> | Yes| Callback used to return the result.| 1316 1317**Example** 1318 1319 ```js 1320 downloadTask.restore((err, result)=>{ 1321 if (err) { 1322 console.error(`Failed to resume the download task. Code: ${err.code}, message: ${err.message}`); 1323 return; 1324 } 1325 console.info('Succeeded in resuming the download task.'); 1326 }); 1327 ``` 1328 1329 1330### remove<sup>(deprecated)</sup> 1331 1332remove(): Promise<boolean> 1333 1334Removes this download task. This API uses a promise to return the result. 1335 1336> **NOTE** 1337> 1338> This API is deprecated since API version 9. You are advised to use [delete<sup>9+</sup>](#delete9-2). 1339 1340**Required permissions**: ohos.permission.INTERNET 1341 1342**System capability**: SystemCapability.MiscServices.Download 1343 1344**Return value** 1345 1346 | Type| Description| 1347 | -------- | -------- | 1348 | Promise<boolean> | Promise used to return the task removal result.| 1349 1350**Example** 1351 1352 ```js 1353 downloadTask.remove().then((result) => { 1354 console.info('Succeeded in removing the download task.'); 1355 }).catch ((err) => { 1356 console.error(`Failed to remove the download task. Code: ${err.code}, message: ${err.message}`); 1357 }); 1358 ``` 1359 1360 1361### remove<sup>(deprecated)</sup> 1362 1363remove(callback: AsyncCallback<boolean>): void 1364 1365Removes this download task. This API uses an asynchronous callback to return the result. 1366 1367> **NOTE** 1368> 1369> This API is deprecated since API version 9. You are advised to use [delete<sup>9+</sup>](#delete9-3). 1370 1371**Required permissions**: ohos.permission.INTERNET 1372 1373**System capability**: SystemCapability.MiscServices.Download 1374 1375**Parameters** 1376 1377 | Name| Type| Mandatory| Description| 1378 | -------- | -------- | -------- | -------- | 1379 | callback | AsyncCallback<boolean> | Yes| Callback used to return the task deletion result.| 1380 1381**Example** 1382 1383 ```js 1384 downloadTask.remove((err, result)=>{ 1385 if(err) { 1386 console.error(`Failed to remove the download task. Code: ${err.code}, message: ${err.message}`); 1387 return; 1388 } 1389 console.info('Succeeded in removing the download task.'); 1390 }); 1391 ``` 1392 1393 1394### query<sup>(deprecated)</sup> 1395 1396query(): Promise<DownloadInfo> 1397 1398Queries this download task. This API uses a promise to return the result. 1399 1400> **NOTE** 1401> 1402> This API is supported since API version 7 and is deprecated since API version 9. You are advised to use [getTaskInfo<sup>9+</sup>](#gettaskinfo9). 1403 1404**Required permissions**: ohos.permission.INTERNET 1405 1406**System capability**: SystemCapability.MiscServices.Download 1407 1408**Return value** 1409 1410 | Type| Description| 1411 | -------- | -------- | 1412 | Promise<[DownloadInfo](#downloadinfo7)> | Promise used to return the download task information.| 1413 1414**Example** 1415 1416 ```js 1417 downloadTask.query().then((downloadInfo) => { 1418 console.info('Succeeded in querying the download task.') 1419 }) .catch((err) => { 1420 console.error(`Failed to query the download task. Code: ${err.code}, message: ${err.message}`) 1421 }); 1422 ``` 1423 1424 1425### query<sup>(deprecated)</sup> 1426 1427query(callback: AsyncCallback<DownloadInfo>): void 1428 1429Queries this download task. This API uses an asynchronous callback to return the result. 1430 1431> **NOTE** 1432> 1433> This API is supported since API version 7 and is deprecated since API version 9. You are advised to use [getTaskInfo<sup>9+</sup>](#gettaskinfo9-1). 1434 1435**Required permissions**: ohos.permission.INTERNET 1436 1437**System capability**: SystemCapability.MiscServices.Download 1438 1439**Parameters** 1440 1441 | Name| Type| Mandatory| Description| 1442 | -------- | -------- | -------- | -------- | 1443 | callback | AsyncCallback<[DownloadInfo](#downloadinfo7)> | Yes| Callback used to return the download task information.| 1444 1445**Example** 1446 1447 ```js 1448 downloadTask.query((err, downloadInfo)=>{ 1449 if(err) { 1450 console.error(`Failed to query the download mimeType. Code: ${err.code}, message: ${err.message}`); 1451 } else { 1452 console.info('Succeeded in querying the download task.'); 1453 } 1454 }); 1455 ``` 1456 1457 1458### queryMimeType<sup>(deprecated)</sup> 1459 1460queryMimeType(): Promise<string> 1461 1462Queries the **MimeType** of this download task. This API uses a promise to return the result. 1463 1464> **NOTE** 1465> 1466> This API is supported since API version 7 and is deprecated since API version 9. You are advised to use [getTaskMimeType<sup>9+</sup>](#gettaskmimetype9). 1467 1468**Required permissions**: ohos.permission.INTERNET 1469 1470**System capability**: SystemCapability.MiscServices.Download 1471 1472**Return value** 1473 1474 | Type| Description| 1475 | -------- | -------- | 1476 | Promise<string> | Promise used to return the **MimeType** of the download task.| 1477 1478**Example** 1479 1480 ```js 1481 downloadTask.queryMimeType().then((data) => { 1482 console.info('Succeededto in querying the download MimeType.'); 1483 }).catch((err) => { 1484 console.error(`Failed to query the download MimeType. Code: ${err.code}, message: ${err.message}`) 1485 }); 1486 ``` 1487 1488 1489### queryMimeType<sup>(deprecated)</sup> 1490 1491queryMimeType(callback: AsyncCallback<string>): void; 1492 1493Queries the **MimeType** of this download task. This API uses an asynchronous callback to return the result. 1494 1495> **NOTE** 1496> 1497> This API is supported since API version 7 and is deprecated since API version 9. You are advised to use [getTaskMimeType<sup>9+</sup>](#gettaskmimetype9-1). 1498 1499**Required permissions**: ohos.permission.INTERNET 1500 1501**System capability**: SystemCapability.MiscServices.Download 1502 1503**Parameters** 1504 1505 | Name| Type| Mandatory| Description| 1506 | -------- | -------- | -------- | -------- | 1507 | callback | AsyncCallback<string> | Yes| Callback used to return the **MimeType** of the download task.| 1508 1509**Example** 1510 1511 ```js 1512 downloadTask.queryMimeType((err, data)=>{ 1513 if(err) { 1514 console.error(`Failed to query the download mimeType. Code: ${err.code}, message: ${err.message}`); 1515 } else { 1516 console.info('Succeeded in querying the download mimeType.'); 1517 } 1518 }); 1519 ``` 1520 1521 1522### pause<sup>(deprecated)</sup> 1523 1524pause(): Promise<void> 1525 1526Pauses this download task. This API uses a promise to return the result. 1527 1528> **NOTE** 1529> 1530> This API is supported since API version 7 and is deprecated since API version 9. You are advised to use [suspend<sup>9+</sup>](#suspend9). 1531 1532**Required permissions**: ohos.permission.INTERNET 1533 1534**System capability**: SystemCapability.MiscServices.Download 1535 1536**Return value** 1537 1538 | Type| Description| 1539 | -------- | -------- | 1540 | Promise<void> | Promise used to return the download task pause result.| 1541 1542**Example** 1543 1544 ```js 1545 downloadTask.pause().then((result) => { 1546 console.info('Succeeded in pausing the download task.'); 1547 }).catch((err) => { 1548 console.error(`Failed to pause the download task. Code: ${err.code}, message: ${err.message}`); 1549 }); 1550 ``` 1551 1552 1553### pause<sup>(deprecated)</sup> 1554 1555pause(callback: AsyncCallback<void>): void 1556 1557> **NOTE** 1558> 1559> This API is supported since API version 7 and is deprecated since API version 9. You are advised to use [suspend<sup>9+</sup>](#suspend9-1). 1560 1561Pauses this download task. This API uses an asynchronous callback to return the result. 1562 1563**Required permissions**: ohos.permission.INTERNET 1564 1565**System capability**: SystemCapability.MiscServices.Download 1566 1567**Parameters** 1568 1569 | Name| Type| Mandatory| Description| 1570 | -------- | -------- | -------- | -------- | 1571 | callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1572 1573**Example** 1574 1575 ```js 1576 downloadTask.pause((err, result)=>{ 1577 if(err) { 1578 console.error(`Failed to pause the download task. Code: ${err.code}, message: ${err.message}`); 1579 return; 1580 } 1581 console.info('Succeeded in pausing the download task.'); 1582 }); 1583 ``` 1584 1585 1586### resume<sup>(deprecated)</sup> 1587 1588resume(): Promise<void> 1589 1590Resumes this download task. This API uses a promise to return the result. 1591 1592> **NOTE** 1593> 1594> This API is supported since API version 7 and is deprecated since API version 9. You are advised to use [restore<sup>9+</sup>](#restore9). 1595 1596**Required permissions**: ohos.permission.INTERNET 1597 1598**System capability**: SystemCapability.MiscServices.Download 1599 1600**Return value** 1601 1602 | Type| Description| 1603 | -------- | -------- | 1604 | Promise<void> | Promise used to return the result.| 1605 1606**Example** 1607 1608 ```js 1609 downloadTask.resume().then((result) => { 1610 console.info('Succeeded in resuming the download task.') 1611 }).catch((err) => { 1612 console.error(`Failed to resume the download task. Code: ${err.code}, message: ${err.message}`); 1613 }); 1614 ``` 1615 1616 1617### resume<sup>(deprecated)</sup> 1618 1619resume(callback: AsyncCallback<void>): void 1620 1621> **NOTE** 1622> 1623> This API is supported since API version 7 and is deprecated since API version 9. You are advised to use [restore<sup>9+</sup>](#restore9-1). 1624 1625Resumes this download task. This API uses an asynchronous callback to return the result. 1626 1627**Required permissions**: ohos.permission.INTERNET 1628 1629**System capability**: SystemCapability.MiscServices.Download 1630 1631**Parameters** 1632 1633 | Name| Type| Mandatory| Description| 1634 | -------- | -------- | -------- | -------- | 1635 | callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1636 1637**Example** 1638 1639 ```js 1640 downloadTask.resume((err, result)=>{ 1641 if (err) { 1642 console.error(`Failed to resume the download task. Code: ${err.code}, message: ${err.message}`); 1643 return; 1644 } 1645 console.info('Succeeded in resuming the download task.'); 1646 }); 1647 ``` 1648 1649 1650## DownloadConfig 1651Defines the download task configuration. 1652 1653**Required permissions**: ohos.permission.INTERNET 1654 1655**System capability**: SystemCapability.MiscServices.Download 1656 1657| Name| Type| Mandatory| Description| 1658| -------- | -------- | -------- | -------- | 1659| url | string | Yes| Resource URL.| 1660| header | Object | No| HTTPS flag header to be included in the download request.<br>The **X-TLS-Version** parameter in **header** specifies the TLS version to be used. If this parameter is not set, the CURL_SSLVERSION_TLSv1_2 version is used. Available options are as follows:<br>CURL_SSLVERSION_TLSv1_0<br>CURL_SSLVERSION_TLSv1_1<br>CURL_SSLVERSION_TLSv1_2<br>CURL_SSLVERSION_TLSv1_3<br>The **X-Cipher-List** parameter in **header** specifies the cipher suite list to be used. If this parameter is not specified, the secure cipher suite list is used. Available options are as follows:<br>- The TLS 1.2 cipher suite list includes the following ciphers:<br>TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,<br>TLS_DHE_DSS_WITH_AES_128_GCM_SHA256,TLS_DSS_RSA_WITH_AES_256_GCM_SHA384,<br>TLS_PSK_WITH_AES_256_GCM_SHA384,TLS_DHE_PSK_WITH_AES_128_GCM_SHA256,<br>TLS_DHE_PSK_WITH_AES_256_GCM_SHA384,TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256,<br>TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,<br>TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,<br>TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,<br>TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256,TLS_ECDHE_PSK_WITH_AES_256_GCM_SHA384,<br>TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256,TLS_DHE_RSA_WITH_AES_128_CCM,<br>TLS_DHE_RSA_WITH_AES_256_CCM,TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,<br>TLS_PSK_WITH_AES_256_CCM,TLS_DHE_PSK_WITH_AES_128_CCM,<br>TLS_DHE_PSK_WITH_AES_256_CCM,TLS_ECDHE_ECDSA_WITH_AES_128_CCM,<br>TLS_ECDHE_ECDSA_WITH_AES_256_CCM,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256<br>- The TLS 1.3 cipher suite list includes the following ciphers:<br>TLS_AES_128_GCM_SHA256,TLS_AES_256_GCM_SHA384,TLS_CHACHA20_POLY1305_SHA256,TLS_AES_128_CCM_SHA256<br>- The TLS 1.3 cipher suite list adds the Chinese national cryptographic algorithm:<br>TLS_SM4_GCM_SM3,TLS_SM4_CCM_SM3 | 1661| enableMetered | boolean | No| Whether download is allowed on a metered connection. The default value is **false**. In general cases, a mobile data connection is metered, while a Wi-Fi connection is not.<br>- **true**: allowed<br>- **false**: not allowed| 1662| enableRoaming | boolean | No| Whether download is allowed on a roaming network. The default value is **false**.<br>- **true**: allowed<br>- **false**: not allowed| 1663| description | string | No| Description of the download session.| 1664| filePath<sup>7+</sup> | string | No| Path where the downloaded file is stored.<br>- In the FA model, use [context](js-apis-inner-app-context.md#contextgetcachedir) to obtain the cache directory of the application, for example, **\${featureAbility.getContext().getFilesDir()}/test.txt\**, and store the file in this directory.<br>- In the stage model, use [AbilityContext](js-apis-inner-application-context.md) to obtain the file path, for example, **\${globalThis.abilityContext.tempDir}/test.txt\**, and store the file in this path.| 1665| networkType | number | No| Network type allowed for the download. The default value is **NETWORK_MOBILE and NETWORK_WIFI**.<br>- NETWORK_MOBILE: 0x00000001<br>- NETWORK_WIFI: 0x00010000| 1666| title | string | No| Download task name.| 1667| background<sup>9+</sup> | boolean | No| Whether to enable background task notification so that the download status is displayed in the notification panel. The default value is false.| 1668 1669 1670## DownloadInfo<sup>7+</sup> 1671Defines the download task information, which is the callback parameter of the [getTaskInfo<sup>9+</sup>](#gettaskinfo9) API. 1672 1673**Required permissions**: ohos.permission.INTERNET 1674 1675**System capability**: SystemCapability.MiscServices.Download 1676 1677| Name| Type|Mandatory| Description| 1678| -------- | -------- | -------- | -------- | 1679| downloadId | number |Yes| ID of the download task.| 1680| failedReason | number|Yes| Cause of the download failure. The value can be any constant in [Download Error Codes](#download-error-codes).| 1681| fileName | string |Yes| Name of the downloaded file.| 1682| filePath | string |Yes| URI of the saved file.| 1683| pausedReason | number |Yes| Cause of download pause. The value can be any constant in [Causes of Download Pause](#causes-of-download-pause).| 1684| status | number |Yes| Download task status code. The value can be any constant in [Download Task Status Codes](#download-task-status-codes).| 1685| targetURI | string |Yes| URI of the downloaded file.| 1686| downloadTitle | string |Yes| Name of the download task.| 1687| downloadTotalBytes | number |Yes| Total size of the files to download, in bytes.| 1688| description | string |Yes| Description of the download task.| 1689| downloadedBytes | number |Yes| Size of the files downloaded, in bytes.| 1690 1691## Action<sup>10+</sup> 1692 1693Defines action options. 1694 1695**System capability**: SystemCapability.Request.FileTransferAgent 1696 1697| Name| Value|Description| 1698| -------- | -------- |-------- | 1699| DOWNLOAD | 0 |Download.| 1700| UPLOAD | 1 |Upload.| 1701 1702 1703## Mode<sup>10+</sup> 1704Defines mode options. 1705 1706**System capability**: SystemCapability.Request.FileTransferAgent 1707 1708| Name| Value|Description| 1709| -------- | -------- |-------- | 1710| BACKGROUND | 0 |Background task.| 1711| FOREGROUND | 1 |Foreground task.| 1712 1713## Network<sup>10+</sup> 1714 1715Defines network options. 1716 1717**System capability**: SystemCapability.Request.FileTransferAgent 1718 1719| Name| Value|Description| 1720| -------- | -------- |-------- | 1721| ANY | 0 |Network of any type.| 1722| WIFI | 1 |Wi-Fi network.| 1723| CELLULAR | 2 |Cellular data network.| 1724 1725## FileSpec<sup>10+</sup> 1726Provides the file information of a table item. 1727 1728**System capability**: SystemCapability.Request.FileTransferAgent 1729 1730| Name| Type| Mandatory| Description| 1731| -------- | -------- | -------- | -------- | 1732| path | string | Yes| File path. The options are as follows:<br>- Relative path in the cache folder of the invoker.<br>- URI (applicable when the application has the permission to access the URI).| 1733| mimeType | string | No| MIME type of the file. The default value is as follows:<br>- During an upload, the value is obtained from the file name or URI suffix.<br>- During a download, the value is **Content-Type** when there is response and **octet-stream** otherwise.| 1734| filename | string | No| File name. The default value is obtained from the file path.| 1735| extras | Object | No| Additional information of the file.| 1736 1737 1738## FormItem<sup>10+</sup> 1739Describes the form item of a task. 1740 1741**System capability**: SystemCapability.Request.FileTransferAgent 1742 1743| Name| Type| Mandatory| Description| 1744| -------- | -------- | -------- | -------- | 1745| name | string | Yes| Form parameter name.| 1746| value | string \| [FileSpec](#filespec10) \| Array<[FileSpec](#filespec10)> | Yes| Form parameter value.| 1747 1748 1749## Config<sup>10+</sup> 1750Provides the configuration information of an upload or download task. 1751 1752**System capability**: SystemCapability.Request.FileTransferAgent 1753 1754| Name| Type| Mandatory| Description| 1755| -------- | -------- | -------- | -------- | 1756| action | [Action](#action10) | Yes| Task action.<br>- **UPLOAD**<br>- **DOWNLOAD**| 1757| url | string | Yes| Resource URL. The value contains a maximum of 2048 characters.| 1758| title | string | No| Task title. The value contains a maximum of 256 characters. The default value is a null string.| 1759| description | string | No| Task description. The value contains a maximum of 1024 characters. The default value is a null string.| 1760| mode | [Mode](#mode10) | No| Task mode. The default mode is background.<br>- For a foreground task, a callback is used for notification.<br>- For a background task, the system notification and network connection features (detection, recovery, and automatic retry) are provided.| 1761| overwrite | boolean | No| Whether to overwrite an existing file during the download. The default value is **false**.<br>- **true**: Overwrite the existing file.<br>- **false**: Do not overwrite the existing file. In this case, the download fails.| 1762| method | string | No| Standard HTTP method for the task. The value can be **GET**, **POST**, or **PUT**, which is case-insensitive.<br>- If the task is an upload, use **PUT** or **POST**. The default value is **PUT**.<br>- If the task is a download, use **GET** or **POST**. The default value is **GET**.| 1763| headers | object | No| HTTPS headers to be included in the task.<br>- If the task is an upload, the default **Content-Type** is **multipart/form-data**.<br>- If the task is a download, the default **Content-Type** is **application/json**.| 1764| data | string \| Array<[FormItem](#formitem10)> | No| Task data.<br>- If the task is a download, the value is a string, typically in JSON format (an object will be converted to a JSON string); the default value is null.<br>- If the task is an upload, the value is Array<[FormItem](#formitem10)>; the default value is null.| 1765| saveas | string | No| Path for storing downloaded files. The options are as follows:<br>- Relative path in the cache folder of the invoker, for example, **"./xxx/yyy/zzz.html"** and **"xxx/yyy/zzz.html"**.<br>- URI (applicable when the application has the permission to access the URI), for example, **"datashare://bundle/xxx/yyy/zzz.html"**. This option is not supported currently.<br>The default value is a relative path in the cache folder of the application.| 1766| network | [Network](#network10) | No| Network used for the task. The default value is **ANY** (Wi-Fi or cellular).| 1767| metered | boolean | No| Whether the task is allowed on a metered connection. The default value is **false**.<br>- **true**: The task is allowed on a metered connection.<br>- **false**: The task is not allowed on a metered connection.| 1768| roaming | boolean | No| Whether the task is allowed on a roaming network. The default value is **true**.<br>- **true**: The task is allowed on a roaming network.<br>- **false**: The task is not allowed on a roaming network.| 1769| retry | boolean | No| Whether automatic retry is enabled for the task. This parameter is only applicable to background tasks. The default value is **true**.<br>- **true**: Automatic retry is enabled for the task.<br>- **-false**: Automatic retry is not enabled for the task.| 1770| redirect | boolean | No| Whether redirection is allowed. The default value is **true**.<br>- **true**: Redirection is allowed.<br>- **false**: Redirection is not allowed.| 1771| index | number | No| Path index of the task. It is usually used for resumable downloads. The default value is **0**.| 1772| begins | number | No| File start point of the task. It is usually used for resumable downloads. The default value is **0**. The value is a closed interval.<br>- If the task is a download, the value is obtained by sending an HTTP range request to read the start position when the server starts to download files.<br>- If the task is an upload, the value is obtained at the beginning of the upload.| 1773| ends | number | No| File end point of the task. It is usually used for resumable downloads. The default value is **-1**. The value is a closed interval.<br>- If the task is a download, the value is obtained by sending an HTTP range request to read the end position when the server starts to download files.<br>- If the task is an upload, the value is obtained at the end of the upload.| 1774| gauge | boolean | No| Whether to send progress notifications. This parameter applies only to background tasks. The default value is **false**.<br>- **false**: Progress notifications are not sent. This means that a notification is sent only to indicate the result of the total task.<br>- **true**: Progress notifications are sent to indicate the result of each file.| 1775| precise | boolean | No| - If this parameter is set to **true**, the task fails when the file size cannot be obtained.<br>- If this parameter is set to **false**, the task continues when the file size is set to **-1**.<br>The default value is **false**.| 1776| token | string | No| Token of the task. If the task has a token configured, this token is required for query of the task. The value contains 8 to 2048 bytes. This parameter is left empty by default.| 1777| extras | object | No| Additional information of the task. This parameter is left empty by default.| 1778 1779## State<sup>10+</sup> 1780 1781Defines the current task status. 1782 1783**System capability**: SystemCapability.Request.FileTransferAgent 1784 1785| Name| Value|Description| 1786| -------- | -------- |-------- | 1787| INITIALIZED | 0x00 |The task is initialized based on the configuration specified in [Conf](#config10).| 1788| WAITING | 0x10 |The task lacks resources for running or the resources for retries do not match the network status.| 1789| RUNNING | 0x20 |The task is being executed.| 1790| RETRYING | 0x21 |The task has failed at least once and is being executed again.| 1791| PAUSED | 0x30 |The task is suspended and will be resumed later.| 1792| STOPPED | 0x31 |The task is stopped.| 1793| COMPLETED | 0x40 |The task is complete.| 1794| FAILED | 0x41 |The task fails.| 1795| REMOVED | 0x50 |The task is removed.| 1796 1797 1798## Progress<sup>10+</sup> 1799Describes the data structure of the task progress. 1800 1801**System capability**: SystemCapability.Request.FileTransferAgent 1802 1803| Name| Type| Mandatory| Description| 1804| -------- | -------- | -------- | -------- | 1805| state | [State](#state10) | Yes| Current task status.| 1806| index | number | Yes| Index of the file that is being processed in the task.| 1807| processed | number | Yes| Size of processed data in the current file in the task, in bytes.| 1808| sizes | Array<number> | Yes| Size of files in the task, in bytes.| 1809| extras | object | No| Extra information of the task, for example, the header and body of the response from the server.| 1810 1811 1812## Faults<sup>10+</sup> 1813 1814Defines the cause of a task failure. 1815 1816**System capability**: SystemCapability.Request.FileTransferAgent 1817 1818| Name| Value|Description| 1819| -------- | -------- |-------- | 1820| OTHERS | 0xFF |Other fault.| 1821| DISCONNECTED | 0x00 |Network disconnection.| 1822| TIMEOUT | 0x10 |Timeout.| 1823| PROTOCOL | 0x20 |Protocol error, for example, an internal server error (500) or a data range that cannot be processed (416).| 1824| FSIO | 0x40 |File system I/O error, for example, an error that occurs during the open, search, read, write, or close operation.| 1825 1826 1827## Filter<sup>10+</sup> 1828Defines the filter criteria. 1829 1830**System capability**: SystemCapability.Request.FileTransferAgent 1831 1832| Name| Type| Mandatory| Description| 1833| -------- | -------- | -------- | -------- | 1834| bundle | string | No| Bundle name of an application.<br>**System API**: This is a system API.| 1835| before | number | No| Unix timestamp of the end time, in milliseconds. The default value is the invoking time.| 1836| after | number | No| Unix timestamp of the start time, in milliseconds. The default value is the invoking time minus 24 hours.| 1837| state | [State](#state10) | No| Task state.| 1838| action | [Action](#action10) | No| Task action.<br>- **UPLOAD**<br>- **DOWNLOAD**| 1839| mode | [Mode](#mode10) | No| Task mode.<br>- **FOREGROUND**<br>- **BACKGROUND**| 1840 1841## TaskInfo<sup>10+</sup> 1842Defines the data structure of the task information for query. The fields available vary depending on the query type. 1843 1844**System capability**: SystemCapability.Request.FileTransferAgent 1845 1846| Name| Type| Mandatory| Description| 1847| -------- | -------- | -------- | -------- | 1848| uid | string | No| UID of the application. It is only available for query by system applications.<br>**System API**: This is a system API.| 1849| bundle | string | No| Bundle name of the application. It is only available for query by system applications.<br>**System API**: This is a system API.| 1850| saveas | string | No| Path for storing downloaded files. The options are as follows:<br>- Relative path in the cache folder of the invoker, for example, **"./xxx/yyy/zzz.html"** and **"xxx/yyy/zzz.html"**.<br>- URI (applicable when the application has the permission to access the URI), for example, **"datashare://bundle/xxx/yyy/zzz.html"**. This option is not supported currently.<br>The default value is a relative path in the cache folder of the application.| 1851| url | string | No| Task URL.<br>- It can be obtained through [request.agent.show<sup>10+</sup>](#requestagentshow10-1), [request.agent.touch<sup>10+</sup>](#requestagenttouch10-1), or [request.agent.query<sup>10+</sup>](#requestagentquery10-1). When [request.agent.query<sup>10+</sup>](#requestagentquery10-1) is used, an empty string is returned.| 1852| data | string \| Array<[FormItem](#formitem10)> | No| Task value.<br>- It can be obtained through [request.agent.show<sup>10+</sup>](#requestagentshow10-1), [request.agent.touch<sup>10+</sup>](#requestagenttouch10-1), or [request.agent.query<sup>10+</sup>](#requestagentquery10-1). When [request.agent.query<sup>10+</sup>](#requestagentquery10-1) is used, an empty string is returned.| 1853| tid | string | Yes| Task ID.| 1854| title | string | Yes| Task title.| 1855| description | string | Yes| Task description.| 1856| action | [Action](#action10) | Yes| Task action.<br>- **UPLOAD**<br>- **DOWNLOAD**| 1857| mode | [Mode](#mode10) | Yes| Task mode.<br>- **FOREGROUND**<br>- **BACKGROUND**| 1858| mimeType | string | Yes| MIME type in the task configuration.| 1859| progress | [Progress](#progress10) | Yes| Task progress.| 1860| gauge | boolean | Yes| Whether to send progress notifications. This parameter applies only to background tasks.| 1861| ctime | number | Yes| Unix timestamp when the task is created, in milliseconds. The value is generated by the system of the current device.<br>Note: When [request.agent.search<sup>10+</sup>](#requestagentsearch10-1) is used for query, this value must be within the range of [after,before] for the task ID to be obtained. For details about **before** and **after**, see [Filter](#filter10). 1862| mtime | number | Yes| Unix timestamp when the task state changes, in milliseconds. The value is generated by the system of the current device.| 1863| retry | boolean | Yes| Whether automatic retry is enabled for the task. This parameter applies only to background tasks.| 1864| tries | number | Yes| Number of retries of the task.| 1865| faults | [Faults](#faults10) | Yes| Failure cause of the task.<br>- **OTHERS**: other fault.<br>- **DISCONNECT**: network disconnection.<br>- **TIMEOUT**: timeout.<br>- **PROTOCOL**: protocol error.<br>- **FSIO**: file system I/O error.| 1866| reason | string | Yes| Reason why the task is waiting, failed, stopped, or paused.| 1867| extras | string | No| Extra information of the task| 1868 1869 1870## Task<sup>10+</sup> 1871Implements an upload or download task. Before using this API, you must obtain a **Task** object, from a promise through [request.agent.create<sup>10+</sup>](#requestagentcreate10-1) or from a callback through [request.agent.create<sup>10+</sup>](#requestagentcreate10). 1872 1873### Attributes 1874Task attributes include the task ID and task configuration. 1875 1876**System capability**: SystemCapability.Request.FileTransferAgent 1877 1878| Name| Type| Mandatory| Description| 1879| -------- | -------- | -------- | -------- | 1880| tid | string | Yes| Task ID, which is unique in the system and is automatically generated by the system.| 1881| config | [Config](#config10) | Yes| Task configuration.| 1882 1883 1884### on('progress')<sup>10+</sup> 1885 1886on(event: 'progress', callback: (progress: Progress) => void): void 1887 1888Subscribes to task progress events. 1889 1890**System capability**: SystemCapability.Request.FileTransferAgent 1891 1892**Parameters** 1893 1894 | Name| Type| Mandatory| Description| 1895 | -------- | -------- | -------- | -------- | 1896 | event | string | Yes| Type of the event to subscribe to.<br>The value is **'progress'**, indicating the task progress.| 1897 | callback | function | Yes| Callback used to return the data structure of the task progress.| 1898 1899**Error codes** 1900 1901For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 1902 1903 | ID| Error Message| 1904 | -------- | -------- | 1905 | 21900005 | task mode error. | 1906 1907**Example** 1908 1909 ```js 1910 let context; 1911 let attachments = [{ 1912 name: "taskOnTest", 1913 value: { 1914 filename: "taskOnTest.avi", 1915 mimetype: "application/octet-stream", 1916 path: "./taskOnTest.avi", 1917 } 1918 }]; 1919 let config = { 1920 action: request.agent.Action.UPLOAD, 1921 url: 'http://127.0.0.1', 1922 title: 'taskOnTest', 1923 description: 'Sample code for event listening', 1924 mode: request.agent.Mode.BACKGROUND, 1925 overwrite: false, 1926 method: "PUT", 1927 data: attachments, 1928 saveas: "./", 1929 network: request.agent.Network.CELLULAR, 1930 metered: false, 1931 roaming: true, 1932 retry: true, 1933 redirect: true, 1934 index: 0, 1935 begins: 0, 1936 ends: -1, 1937 gauge: false, 1938 precise: false, 1939 token: "it is a secret" 1940 }; 1941 let createOnCallback = (progress) => { 1942 console.info('upload task completed.'); 1943 }; 1944 request.agent.create(context, config).then((task)=> { 1945 task.on('progress', createOnCallback); 1946 console.info(`Succeeded in creating a upload task. result: ${task.tid}`); 1947 }).catch((err) => { 1948 console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`); 1949 }); 1950 ``` 1951 1952> **NOTE** 1953> 1954> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 1955 1956### on('completed')<sup>10+</sup> 1957 1958on(event: 'completed', callback: (progress: Progress) => void): void 1959 1960Subscribes to task completion events. 1961 1962**System capability**: SystemCapability.Request.FileTransferAgent 1963 1964**Parameters** 1965 1966 | Name| Type| Mandatory| Description| 1967 | -------- | -------- | -------- | -------- | 1968 | event | string | Yes| Type of the event to subscribe to.<br>The value is **'completed'**, indicating task completion.| 1969 | callback | function | Yes| Callback used to return the data structure of the task progress.| 1970 1971**Error codes** 1972 1973For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 1974 1975 | ID| Error Message| 1976 | -------- | -------- | 1977 | 21900005 | task mode error. | 1978 1979**Example** 1980 1981 ```js 1982 let context; 1983 let attachments = [{ 1984 name: "taskOnTest", 1985 value: { 1986 filename: "taskOnTest.avi", 1987 mimetype: "application/octet-stream", 1988 path: "./taskOnTest.avi", 1989 } 1990 }]; 1991 let config = { 1992 action: request.agent.Action.UPLOAD, 1993 url: 'http://127.0.0.1', 1994 title: 'taskOnTest', 1995 description: 'Sample code for event listening', 1996 mode: request.agent.Mode.BACKGROUND, 1997 overwrite: false, 1998 method: "PUT", 1999 data: attachments, 2000 saveas: "./", 2001 network: request.agent.Network.CELLULAR, 2002 metered: false, 2003 roaming: true, 2004 retry: true, 2005 redirect: true, 2006 index: 0, 2007 begins: 0, 2008 ends: -1, 2009 gauge: false, 2010 precise: false, 2011 token: "it is a secret" 2012 }; 2013 let createOnCallback = (progress) => { 2014 console.info('upload task completed.'); 2015 }; 2016 request.agent.create(context, config).then((task)=> { 2017 task.on('completed', createOnCallback); 2018 console.info(`Succeeded in creating a upload task. result: ${task.tid}`); 2019 }).catch((err) => { 2020 console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`); 2021 }); 2022 ``` 2023 2024> **NOTE** 2025> 2026> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2027 2028### on('failed')<sup>10+</sup> 2029 2030on(event: 'failed', callback: (progress: Progress) => void): void 2031 2032Subscribes to task failure events. 2033 2034**System capability**: SystemCapability.Request.FileTransferAgent 2035 2036**Parameters** 2037 2038 | Name| Type| Mandatory| Description| 2039 | -------- | -------- | -------- | -------- | 2040 | event | string | Yes| Type of the event to subscribe to.<br>The value is **'failed'**, indicating task failure.| 2041 | callback | function | Yes| Callback used to return the data structure of the task progress.| 2042 2043**Error codes** 2044 2045For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2046 2047 | ID| Error Message| 2048 | -------- | -------- | 2049 | 21900005 | task mode error. | 2050 2051**Example** 2052 2053 ```js 2054 let context; 2055 let attachments = [{ 2056 name: "taskOnTest", 2057 value: { 2058 filename: "taskOnTest.avi", 2059 mimetype: "application/octet-stream", 2060 path: "./taskOnTest.avi", 2061 } 2062 }]; 2063 let config = { 2064 action: request.agent.Action.UPLOAD, 2065 url: 'http://127.0.0.1', 2066 title: 'taskOnTest', 2067 description: 'Sample code for event listening', 2068 mode: request.agent.Mode.BACKGROUND, 2069 overwrite: false, 2070 method: "PUT", 2071 data: attachments, 2072 saveas: "./", 2073 network: request.agent.Network.CELLULAR, 2074 metered: false, 2075 roaming: true, 2076 retry: true, 2077 redirect: true, 2078 index: 0, 2079 begins: 0, 2080 ends: -1, 2081 gauge: false, 2082 precise: false, 2083 token: "it is a secret" 2084 }; 2085 let createOnCallback = (progress) => { 2086 console.info('upload task completed.'); 2087 }; 2088 request.agent.create(context, config).then((task)=> { 2089 task.on('failed', createOnCallback); 2090 console.info(`Succeeded in creating a upload task. result: ${task.tid}`); 2091 }).catch((err) => { 2092 console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`); 2093 }); 2094 ``` 2095 2096> **NOTE** 2097> 2098> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2099 2100 2101### off('progress')<sup>10+</sup> 2102 2103off(event: 'progress', callback?: (progress: Progress) => void): void 2104 2105Unsubscribes from task progress events. 2106 2107**System capability**: SystemCapability.Request.FileTransferAgent 2108 2109**Parameters** 2110 2111 | Name| Type| Mandatory| Description| 2112 | -------- | -------- | -------- | -------- | 2113 | event | string | Yes| Type of the event to subscribe to.<br>The value is **'progress'**, indicating the task progress.| 2114 | callback | function | No| Callback used to return the data structure of the task progress.| 2115 2116**Error codes** 2117 2118For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2119 2120 | ID| Error Message| 2121 | -------- | -------- | 2122 | 21900005 | task mode error. | 2123 2124**Example** 2125 2126 ```js 2127 let context; 2128 let attachments = [{ 2129 name: "taskOffTest", 2130 value: { 2131 filename: "taskOffTest.avi", 2132 mimetype: "application/octet-stream", 2133 path: "./taskOffTest.avi", 2134 } 2135 }]; 2136 let config = { 2137 action: request.agent.Action.UPLOAD, 2138 url: 'http://127.0.0.1', 2139 title: 'taskOffTest', 2140 description: 'Sample code for event listening', 2141 mode: request.agent.Mode.BACKGROUND, 2142 overwrite: false, 2143 method: "PUT", 2144 data: attachments, 2145 saveas: "./", 2146 network: request.agent.Network.CELLULAR, 2147 metered: false, 2148 roaming: true, 2149 retry: true, 2150 redirect: true, 2151 index: 0, 2152 begins: 0, 2153 ends: -1, 2154 gauge: false, 2155 precise: false, 2156 token: "it is a secret" 2157 }; 2158 let createOffCallback = (progress) => { 2159 console.info('upload task completed.'); 2160 }; 2161 request.agent.create(context, config).then((task)=> { 2162 task.on('progress', createOffCallback); 2163 task.off('progress', createOffCallback); 2164 console.info(`Succeeded in creating a upload task. result: ${task.tid}`); 2165 }).catch((err) => { 2166 console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`); 2167 }); 2168 ``` 2169 2170> **NOTE** 2171> 2172> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2173 2174### off('completed')<sup>10+</sup> 2175 2176off(event: 'completed', callback?: (progress: Progress) => void): void 2177 2178Unsubscribes from task completion events. 2179 2180**System capability**: SystemCapability.Request.FileTransferAgent 2181 2182**Parameters** 2183 2184 | Name| Type| Mandatory| Description| 2185 | -------- | -------- | -------- | -------- | 2186 | event | string | Yes| Type of the event to subscribe to.<br>The value is **'completed'**, indicating task completion.| 2187 | callback | function | No| Callback used to return the data structure of the task progress.| 2188 2189**Error codes** 2190 2191For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2192 2193 | ID| Error Message| 2194 | -------- | -------- | 2195 | 21900005 | task mode error. | 2196 2197**Example** 2198 2199 ```js 2200 let context; 2201 let attachments = [{ 2202 name: "taskOffTest", 2203 value: { 2204 filename: "taskOffTest.avi", 2205 mimetype: "application/octet-stream", 2206 path: "./taskOffTest.avi", 2207 } 2208 }]; 2209 let config = { 2210 action: request.agent.Action.UPLOAD, 2211 url: 'http://127.0.0.1', 2212 title: 'taskOffTest', 2213 description: 'Sample code for event listening', 2214 mode: request.agent.Mode.BACKGROUND, 2215 overwrite: false, 2216 method: "PUT", 2217 data: attachments, 2218 saveas: "./", 2219 network: request.agent.Network.CELLULAR, 2220 metered: false, 2221 roaming: true, 2222 retry: true, 2223 redirect: true, 2224 index: 0, 2225 begins: 0, 2226 ends: -1, 2227 gauge: false, 2228 precise: false, 2229 token: "it is a secret" 2230 }; 2231 let createOffCallback = (progress) => { 2232 console.info('upload task completed.'); 2233 }; 2234 request.agent.create(context, config).then((task)=> { 2235 task.on('completed', createOffCallback); 2236 task.off('completed', createOffCallback); 2237 console.info(`Succeeded in creating a upload task. result: ${task.tid}`); 2238 }).catch((err) => { 2239 console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`); 2240 }); 2241 ``` 2242 2243> **NOTE** 2244> 2245> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2246 2247### off('failed')<sup>10+</sup> 2248 2249off(event: 'failed', callback?: (progress: Progress) => void): void 2250 2251Unsubscribes from task failure events. 2252 2253**System capability**: SystemCapability.Request.FileTransferAgent 2254 2255**Parameters** 2256 2257 | Name| Type| Mandatory| Description| 2258 | -------- | -------- | -------- | -------- | 2259 | event | string | Yes| Type of the event to subscribe to.<br>The value is **'failed'**, indicating task failure.| 2260 | callback | function | No| Callback used to return the data structure of the task progress.| 2261 2262**Error codes** 2263 2264For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2265 2266 | ID| Error Message| 2267 | -------- | -------- | 2268 | 21900005 | task mode error. | 2269 2270**Example** 2271 2272 ```js 2273 let context; 2274 let attachments = [{ 2275 name: "taskOffTest", 2276 value: { 2277 filename: "taskOffTest.avi", 2278 mimetype: "application/octet-stream", 2279 path: "./taskOffTest.avi", 2280 } 2281 }]; 2282 let config = { 2283 action: request.agent.Action.UPLOAD, 2284 url: 'http://127.0.0.1', 2285 title: 'taskOffTest', 2286 description: 'Sample code for event listening', 2287 mode: request.agent.Mode.BACKGROUND, 2288 overwrite: false, 2289 method: "PUT", 2290 data: attachments, 2291 saveas: "./", 2292 network: request.agent.Network.CELLULAR, 2293 metered: false, 2294 roaming: true, 2295 retry: true, 2296 redirect: true, 2297 index: 0, 2298 begins: 0, 2299 ends: -1, 2300 gauge: false, 2301 precise: false, 2302 token: "it is a secret" 2303 }; 2304 let createOffCallback = (progress) => { 2305 console.info('upload task completed.'); 2306 }; 2307 request.agent.create(context, config).then((task)=> { 2308 task.on('failed', createOffCallback); 2309 task.off('failed', createOffCallback); 2310 console.info(`Succeeded in creating a upload task. result: ${task.tid}`); 2311 }).catch((err) => { 2312 console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`); 2313 }); 2314 ``` 2315 2316> **NOTE** 2317> 2318> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2319 2320### start<sup>10+</sup> 2321 2322start(callback: AsyncCallback<void>): void 2323 2324Starts this task. This API cannot be used to start an initialized task. This API uses an asynchronous callback to return the result. 2325 2326**Required permissions**: ohos.permission.INTERNET 2327 2328**System capability**: SystemCapability.Request.FileTransferAgent 2329 2330**Parameters** 2331 2332 | Name| Type| Mandatory| Description| 2333 | -------- | -------- | -------- | -------- | 2334 | callback | function | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 2335 2336**Error codes** 2337 2338For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2339 2340 | ID| Error Message| 2341 | -------- | -------- | 2342 | 13400003 | task service ability error. | 2343 | 21900007 | task state error. | 2344 2345**Example** 2346 2347 ```js 2348 let context; 2349 let config = { 2350 action: request.agent.Action.DOWNLOAD, 2351 url: 'http://127.0.0.1', 2352 title: 'taskStartTest', 2353 description: 'Sample code for start the download task', 2354 mode: request.agent.Mode.BACKGROUND, 2355 overwrite: false, 2356 method: "GET", 2357 data: "", 2358 saveas: "./", 2359 network: request.agent.Network.CELLULAR, 2360 metered: false, 2361 roaming: true, 2362 retry: true, 2363 redirect: true, 2364 index: 0, 2365 begins: 0, 2366 ends: -1, 2367 gauge: false, 2368 precise: false, 2369 token: "it is a secret" 2370 }; 2371 request.agent.create(context, config).then((task) => { 2372 task.start((err) => { 2373 if (err) { 2374 console.error(`Failed to start the download task, Code: ${err.code}, message: ${err.message}`); 2375 return; 2376 } 2377 console.info(`Succeeded in starting a download task.`); 2378 }); 2379 console.info(`Succeeded in creating a download task. result: ${task.tid}`); 2380 }).catch((err) => { 2381 console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 2382 }); 2383 ``` 2384 2385> **NOTE** 2386> 2387> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2388 2389### start<sup>10+</sup> 2390 2391start(): Promise<void> 2392 2393Starts this task. This API cannot be used to start an initialized task. This API uses a promise to return the result. 2394 2395**Required permissions**: ohos.permission.INTERNET 2396 2397**System capability**: SystemCapability.Request.FileTransferAgent 2398 2399**Return value** 2400 2401| Type | Description | 2402| ------------------- | ------------------------- | 2403| Promise<void> | Promise that returns no value.| 2404 2405**Error codes** 2406 2407For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2408 2409 | ID| Error Message| 2410 | -------- | -------- | 2411 | 13400003 | task service ability error. | 2412 | 21900007 | task state error. | 2413 2414**Example** 2415 2416 ```js 2417 let context; 2418 let config = { 2419 action: request.agent.Action.DOWNLOAD, 2420 url: 'http://127.0.0.1', 2421 title: 'taskStartTest', 2422 description: 'Sample code for start the download task', 2423 mode: request.agent.Mode.BACKGROUND, 2424 overwrite: false, 2425 method: "GET", 2426 data: "", 2427 saveas: "./", 2428 network: request.agent.Network.CELLULAR, 2429 metered: false, 2430 roaming: true, 2431 retry: true, 2432 redirect: true, 2433 index: 0, 2434 begins: 0, 2435 ends: -1, 2436 gauge: false, 2437 precise: false, 2438 token: "it is a secret" 2439 }; 2440 request.agent.create(context, config).then((task) => { 2441 task.start().then(() => { 2442 console.info(`Succeeded in starting a download task.`); 2443 }).catch((err) => { 2444 console.error(`Failed to start the download task, Code: ${err.code}, message: ${err.message}`); 2445 }); 2446 console.info(`Succeeded in creating a download task. result: ${task.tid}`); 2447 }).catch((err) => { 2448 console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 2449 }); 2450 ``` 2451 2452> **NOTE** 2453> 2454> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2455 2456### pause<sup>10+</sup> 2457 2458pause(callback: AsyncCallback<void>): void 2459 2460Pauses this task. This API can be used to pause a background task that is waiting, running, or retrying. This API uses an asynchronous callback to return the result. 2461 2462**System capability**: SystemCapability.Request.FileTransferAgent 2463 2464**Parameters** 2465 2466 | Name| Type| Mandatory| Description| 2467 | -------- | -------- | -------- | -------- | 2468 | callback | function | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 2469 2470**Error codes** 2471 2472For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2473 2474 | ID| Error Message| 2475 | -------- | -------- | 2476 | 13400003 | task service ability error. | 2477 | 21900005 | task mode error. | 2478 | 21900007 | task state error. | 2479 2480**Example** 2481 2482 ```js 2483 let context; 2484 let config = { 2485 action: request.agent.Action.DOWNLOAD, 2486 url: 'http://127.0.0.1', 2487 title: 'taskPauseTest', 2488 description: 'Sample code for pause the download task', 2489 mode: request.agent.Mode.BACKGROUND, 2490 overwrite: false, 2491 method: "GET", 2492 data: "", 2493 saveas: "./", 2494 network: request.agent.Network.CELLULAR, 2495 metered: false, 2496 roaming: true, 2497 retry: true, 2498 redirect: true, 2499 index: 0, 2500 begins: 0, 2501 ends: -1, 2502 gauge: false, 2503 precise: false, 2504 token: "it is a secret" 2505 }; 2506 request.agent.create(context, config).then((task) => { 2507 task.pause((err) => { 2508 if (err) { 2509 console.error(`Failed to pause the download task, Code: ${err.code}, message: ${err.message}`); 2510 return; 2511 } 2512 console.info(`Succeeded in pausing a download task. `); 2513 }); 2514 console.info(`Succeeded in creating a download task. result: ${task.tid}`); 2515 }).catch((err) => { 2516 console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 2517 }); 2518 ``` 2519 2520 2521### pause<sup>10+</sup> 2522 2523pause(): Promise<void> 2524 2525Pauses this task. This API can be used to pause a background task that is waiting, running, or retrying. This API uses a promise to return the result. 2526 2527**System capability**: SystemCapability.Request.FileTransferAgent 2528 2529**Return value** 2530 2531| Type | Description | 2532| ------------------- | ------------------------- | 2533| Promise<void> | Promise that returns no value.| 2534 2535**Error codes** 2536 2537For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2538 2539 | ID| Error Message| 2540 | -------- | -------- | 2541 | 13400003 | task service ability error. | 2542 | 21900005 | task mode error. | 2543 | 21900007 | task state error. | 2544 2545**Example** 2546 2547 ```js 2548 let context; 2549 let config = { 2550 action: request.agent.Action.DOWNLOAD, 2551 url: 'http://127.0.0.1', 2552 title: 'taskPauseTest', 2553 description: 'Sample code for pause the download task', 2554 mode: request.agent.Mode.BACKGROUND, 2555 overwrite: false, 2556 method: "GET", 2557 data: "", 2558 saveas: "./", 2559 network: request.agent.Network.CELLULAR, 2560 metered: false, 2561 roaming: true, 2562 retry: true, 2563 redirect: true, 2564 index: 0, 2565 begins: 0, 2566 ends: -1, 2567 gauge: false, 2568 precise: false, 2569 token: "it is a secret" 2570 }; 2571 request.agent.create(context, config).then((task) => { 2572 task.pause().then(() => { 2573 console.info(`Succeeded in pausing a download task. `); 2574 }).catch((err) => { 2575 console.error(`Failed to pause the upload task, Code: ${err.code}, message: ${err.message}`); 2576 }); 2577 console.info(`Succeeded in creating a download task. result: ${task.tid}`); 2578 }).catch((err) => { 2579 console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`); 2580 }); 2581 ``` 2582 2583 2584### resume<sup>10+</sup> 2585 2586resume(callback: AsyncCallback<void>): void 2587 2588Resumes this task. This API can be used to resume a paused background task. This API uses an asynchronous callback to return the result. 2589 2590**Required permissions**: ohos.permission.INTERNET 2591 2592**System capability**: SystemCapability.Request.FileTransferAgent 2593 2594**Parameters** 2595 2596 | Name| Type| Mandatory| Description| 2597 | -------- | -------- | -------- | -------- | 2598 | callback | function | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 2599 2600**Error codes** 2601 2602For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2603 2604 | ID| Error Message| 2605 | -------- | -------- | 2606 | 13400003 | task service ability error. | 2607 | 21900005 | task mode error. | 2608 | 21900007 | task state error. | 2609 2610**Example** 2611 2612 ```js 2613 let context; 2614 let config = { 2615 action: request.agent.Action.DOWNLOAD, 2616 url: 'http://127.0.0.1', 2617 title: 'taskResumeTest', 2618 description: 'Sample code for resume the download task', 2619 mode: request.agent.Mode.BACKGROUND, 2620 overwrite: false, 2621 method: "GET", 2622 data: "", 2623 saveas: "./", 2624 network: request.agent.Network.CELLULAR, 2625 metered: false, 2626 roaming: true, 2627 retry: true, 2628 redirect: true, 2629 index: 0, 2630 begins: 0, 2631 ends: -1, 2632 gauge: false, 2633 precise: false, 2634 token: "it is a secret" 2635 }; 2636 request.agent.create(context, config).then((task) => { 2637 task.resume((err) => { 2638 if (err) { 2639 console.error(`Failed to resume the download task, Code: ${err.code}, message: ${err.message}`); 2640 return; 2641 } 2642 console.info(`Succeeded in resuming a download task. `); 2643 }); 2644 console.info(`Succeeded in creating a download task. result: ${task.tid}`); 2645 }).catch((err) => { 2646 console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 2647 }); 2648 ``` 2649 2650 2651### resume<sup>10+</sup> 2652 2653resume(): Promise<void> 2654 2655Resumes this task. This API can be used to resume a paused background task. This API uses a promise to return the result. 2656 2657**Required permissions**: ohos.permission.INTERNET 2658 2659**System capability**: SystemCapability.Request.FileTransferAgent 2660 2661**Return value** 2662 2663| Type | Description | 2664| ------------------- | ------------------------- | 2665| Promise<void> | Promise that returns no value.| 2666 2667**Error codes** 2668 2669For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2670 2671 | ID| Error Message| 2672 | -------- | -------- | 2673 | 13400003 | task service ability error. | 2674 | 21900005 | task mode error. | 2675 | 21900007 | task state error. | 2676 2677**Example** 2678 2679 ```js 2680 let context; 2681 let config = { 2682 action: request.agent.Action.DOWNLOAD, 2683 url: 'http://127.0.0.1', 2684 title: 'taskResumeTest', 2685 description: 'Sample code for resume the download task', 2686 mode: request.agent.Mode.BACKGROUND, 2687 overwrite: false, 2688 method: "GET", 2689 data: "", 2690 saveas: "./", 2691 network: request.agent.Network.CELLULAR, 2692 metered: false, 2693 roaming: true, 2694 retry: true, 2695 redirect: true, 2696 index: 0, 2697 begins: 0, 2698 ends: -1, 2699 gauge: false, 2700 precise: false, 2701 token: "it is a secret" 2702 }; 2703 request.agent.create(context, config).then((task) => { 2704 task.resume().then(() => { 2705 console.info(`Succeeded in resuming a download task. `); 2706 }).catch((err) => { 2707 console.error(`Failed to resume the download task, Code: ${err.code}, message: ${err.message}`); 2708 }); 2709 console.info(`Succeeded in creating a download task. result: ${task.tid}`); 2710 }).catch((err) => { 2711 console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 2712 }); 2713 ``` 2714 2715 2716### stop<sup>10+</sup> 2717 2718stop(callback: AsyncCallback<void>): void 2719 2720Stops this task. This API can be used to stop a running, waiting, or retrying task. This API uses an asynchronous callback to return the result. 2721 2722**System capability**: SystemCapability.Request.FileTransferAgent 2723 2724**Parameters** 2725 2726 | Name| Type| Mandatory| Description| 2727 | -------- | -------- | -------- | -------- | 2728 | callback | function | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 2729 2730**Error codes** 2731 2732For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2733 2734 | ID| Error Message| 2735 | -------- | -------- | 2736 | 13400003 | task service ability error. | 2737 | 21900007 | task state error. | 2738 2739**Example** 2740 2741 ```js 2742 let context; 2743 let config = { 2744 action: request.agent.Action.DOWNLOAD, 2745 url: 'http://127.0.0.1', 2746 title: 'taskStopTest', 2747 description: 'Sample code for stop the download task', 2748 mode: request.agent.Mode.BACKGROUND, 2749 overwrite: false, 2750 method: "GET", 2751 data: "", 2752 saveas: "./", 2753 network: request.agent.Network.CELLULAR, 2754 metered: false, 2755 roaming: true, 2756 retry: true, 2757 redirect: true, 2758 index: 0, 2759 begins: 0, 2760 ends: -1, 2761 gauge: false, 2762 precise: false, 2763 token: "it is a secret" 2764 }; 2765 request.agent.create(context, config).then((task) => { 2766 task.stop((err) => { 2767 if (err) { 2768 console.error(`Failed to stop the download task, Code: ${err.code}, message: ${err.message}`); 2769 return; 2770 } 2771 console.info(`Succeeded in stopping a download task. `); 2772 }); 2773 console.info(`Succeeded in creating a download task. result: ${task.tid}`); 2774 }).catch((err) => { 2775 console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 2776 }); 2777 ``` 2778 2779 2780### stop<sup>10+</sup> 2781 2782stop(): Promise<void> 2783 2784Stops this task. This API can be used to stop a running, waiting, or retrying task. This API uses a promise to return the result. 2785 2786**System capability**: SystemCapability.Request.FileTransferAgent 2787 2788**Return value** 2789 2790| Type | Description | 2791| ------------------- | ------------------------- | 2792| Promise<void> | Promise that returns no value.| 2793 2794**Error codes** 2795 2796For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2797 2798 | ID| Error Message| 2799 | -------- | -------- | 2800 | 13400003 | task service ability error. | 2801 | 21900007 | task state error. | 2802 2803**Example** 2804 2805 ```js 2806 let context; 2807 let config = { 2808 action: request.agent.Action.DOWNLOAD, 2809 url: 'http://127.0.0.1', 2810 title: 'taskStopTest', 2811 description: 'Sample code for stop the download task', 2812 mode: request.agent.Mode.BACKGROUND, 2813 overwrite: false, 2814 method: "GET", 2815 data: "", 2816 saveas: "./", 2817 network: request.agent.Network.CELLULAR, 2818 metered: false, 2819 roaming: true, 2820 retry: true, 2821 redirect: true, 2822 index: 0, 2823 begins: 0, 2824 ends: -1, 2825 gauge: false, 2826 precise: false, 2827 token: "it is a secret" 2828 }; 2829 request.agent.create(context, config).then((task) => { 2830 task.stop().then(() => { 2831 console.info(`Succeeded in stopping a download task. `); 2832 }).catch((err) => { 2833 console.error(`Failed to stop the download task, Code: ${err.code}, message: ${err.message}`); 2834 }); 2835 console.info(`Succeeded in creating a download task. result: ${task.tid}`); 2836 }).catch((err) => { 2837 console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 2838 }); 2839 ``` 2840 2841## request.agent.create<sup>10+</sup> 2842 2843create(context: BaseContext, config: Config, callback: AsyncCallback<Task>): void 2844 2845Creates an upload or download task and adds it to the queue. An application can create a maximum of 10 tasks, and a maximum of 300 tasks can be carried. This API uses an asynchronous callback to return the result. 2846 2847 2848**Required permissions**: ohos.permission.INTERNET 2849 2850**System capability**: SystemCapability.Request.FileTransferAgent 2851 2852**Parameters** 2853 2854 | Name| Type| Mandatory| Description| 2855 | -------- | -------- | -------- | -------- | 2856 | config | [Config](#config10) | Yes| Task configuration.| 2857 | context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.| 2858 | callback | AsyncCallback<[Task](#task10)> | Yes| Callback used to return the configuration about the created task.| 2859 2860**Error codes** 2861 2862For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2863 2864 | ID| Error Message| 2865 | -------- | -------- | 2866 | 13400001 | file operation error. | 2867 | 13400003 | task service ability error. | 2868 | 21900004 | application task queue full error. | 2869 | 21900005 | task mode error. | 2870 2871**Example** 2872 2873 ```js 2874 let context; 2875 let attachments = [{ 2876 name: "reeateTest", 2877 value: { 2878 filename: "reeateTest.avi", 2879 mimetype: "application/octet-stream", 2880 path: "./reeateTest.avi", 2881 } 2882 }]; 2883 let config = { 2884 action: request.agent.Action.UPLOAD, 2885 url: 'http://127.0.0.1', 2886 title: 'reeateTest', 2887 description: 'Sample code for reeate task', 2888 mode: request.agent.Mode.BACKGROUND, 2889 overwrite: false, 2890 method: "PUT", 2891 data: attachments, 2892 saveas: "./", 2893 network: request.agent.Network.CELLULAR, 2894 metered: false, 2895 roaming: true, 2896 retry: true, 2897 redirect: true, 2898 index: 0, 2899 begins: 0, 2900 ends: -1, 2901 gauge: false, 2902 precise: false, 2903 token: "it is a secret" 2904 }; 2905 request.agent.create(context, config, (err, task) => { 2906 if (err) { 2907 console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 2908 return; 2909 } 2910 console.info(`Succeeded in creating a download task. result: ${task.config}`); 2911 }); 2912 ``` 2913 2914> **NOTE** 2915> 2916> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2917 2918## request.agent.create<sup>10+</sup> 2919 2920create(context: BaseContext, config: Config): Promise<Task> 2921 2922Creates an upload or download task and adds it to the queue. An application can create a maximum of 10 tasks, and a maximum of 300 tasks can be carried. This API uses a promise to return the result. 2923 2924 2925**Required permissions**: ohos.permission.INTERNET 2926 2927**System capability**: SystemCapability.Request.FileTransferAgent 2928 2929**Parameters** 2930 2931 | Name| Type| Mandatory| Description| 2932 | -------- | -------- | -------- | -------- | 2933 | context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.| 2934 | config | [Config](#config10) | Yes| Task configuration.| 2935 2936**Return value** 2937 2938| Type | Description | 2939| ------------------- | ------------------------- | 2940| Promise<[Task](#task10)> | Promise used to return the configuration about the created task.| 2941 2942**Error codes** 2943 2944For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 2945 2946 | ID| Error Message| 2947 | -------- | -------- | 2948 | 13400001 | file operation error. | 2949 | 13400003 | task service ability error. | 2950 | 21900004 | application task queue full error. | 2951 | 21900005 | task mode error. | 2952 2953**Example** 2954 2955 ```js 2956 let context; 2957 let attachments = [{ 2958 name: "reeateTest", 2959 value: { 2960 filename: "reeateTest.avi", 2961 mimetype: "application/octet-stream", 2962 path: "./reeateTest.avi", 2963 } 2964 }]; 2965 let config = { 2966 action: request.agent.Action.UPLOAD, 2967 url: 'http://127.0.0.1', 2968 title: 'reeateTest', 2969 description: 'Sample code for reeate task', 2970 mode: request.agent.Mode.BACKGROUND, 2971 overwrite: false, 2972 method: "PUT", 2973 data: attachments, 2974 saveas: "./", 2975 network: request.agent.Network.CELLULAR, 2976 metered: false, 2977 roaming: true, 2978 retry: true, 2979 redirect: true, 2980 index: 0, 2981 begins: 0, 2982 ends: -1, 2983 gauge: false, 2984 precise: false, 2985 token: "it is a secret" 2986 }; 2987 request.agent.create(context, config).then((task)=> { 2988 console.info(`Succeeded in creating a download task. result: ${task.config}`); 2989 }).catch((err) => { 2990 console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 2991 }); 2992 ``` 2993 2994> **NOTE** 2995> 2996> For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 2997 2998## request.agent.remove<sup>10+</sup> 2999 3000remove(id: string, callback: AsyncCallback<void>): void 3001 3002Removes a specified task of the invoker. If the task is being executed, the task is forced to stop. This API uses an asynchronous callback to return the result. 3003 3004**System capability**: SystemCapability.Request.FileTransferAgent 3005 3006**Parameters** 3007 3008 | Name| Type| Mandatory| Description| 3009 | -------- | -------- | -------- | -------- | 3010 | id | string | Yes| Task ID.| 3011 | callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 3012 3013**Error codes** 3014 3015For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3016 3017 | ID| Error Message| 3018 | -------- | -------- | 3019 | 13400003 | task service ability error. | 3020 | 21900006 | task not found error. | 3021 3022**Example** 3023 3024 ```js 3025 request.agent.remove("id", (err) => { 3026 if (err) { 3027 console.error(`Failed to removing a download task, Code: ${err.code}, message: ${err.message}`); 3028 return; 3029 } 3030 console.info(`Succeeded in creating a download task.`); 3031 }); 3032 ``` 3033 3034 3035## request.agent.remove<sup>10+</sup> 3036 3037remove(id: string): Promise<void> 3038 3039Removes a specified task of the invoker. If the task is being executed, the task is forced to stop. This API uses a promise to return the result. 3040 3041**System capability**: SystemCapability.Request.FileTransferAgent 3042 3043**Parameters** 3044 3045 | Name| Type| Mandatory| Description| 3046 | -------- | -------- | -------- | -------- | 3047 | id | string | Yes| Task ID.| 3048 3049**Return value** 3050 3051| Type | Description | 3052| ------------------- | ------------------------- | 3053| Promise<void> | Promise that returns no value.| 3054 3055**Error codes** 3056 3057For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3058 3059 | ID| Error Message| 3060 | -------- | -------- | 3061 | 13400003 | task service ability error. | 3062 | 21900006 | task not found error. | 3063 3064**Example** 3065 3066 ```js 3067 request.agent.remove("id").then(() => { 3068 console.info(`Succeeded in removing a download task. `); 3069 }).catch((err) => { 3070 console.error(`Failed to remove a download task, Code: ${err.code}, message: ${err.message}`); 3071 }); 3072 ``` 3073 3074 3075## request.agent.show<sup>10+</sup> 3076 3077show(id: string, callback: AsyncCallback<TaskInfo>): void 3078 3079Shows the task details based on the task ID. This API uses an asynchronous callback to return the result. 3080 3081**System capability**: SystemCapability.Request.FileTransferAgent 3082 3083**Parameters** 3084 3085 | Name| Type| Mandatory| Description| 3086 | -------- | -------- | -------- | -------- | 3087 | id | string | Yes| Task ID.| 3088 | callback | AsyncCallback<TaskInfo> | Yes| Callback used to return task details.| 3089 3090**Error codes** 3091For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3092 3093 | ID| Error Message| 3094 | -------- | -------- | 3095 | 13400003 | task service ability error. | 3096 | 21900006 | task not found error. | 3097 3098**Example** 3099 3100 ```js 3101 request.agent.show("123456", (err, TaskInfo) => { 3102 if (err) { 3103 console.error(`Failed to show a unload task, Code: ${err.code}, message: ${err.message}`); 3104 return; 3105 } 3106 console.info(`Succeeded in showing a unload task.`); 3107 }); 3108 ``` 3109 3110 3111## request.agent.show<sup>10+</sup> 3112 3113show(id: string): Promise<TaskInfo> 3114 3115Queries the task details based on the task ID. This API uses a promise to return the result. 3116 3117**System capability**: SystemCapability.Request.FileTransferAgent 3118 3119**Parameters** 3120 3121 | Name| Type| Mandatory| Description| 3122 | -------- | -------- | -------- | -------- | 3123 | id | string | Yes| Task ID.| 3124 3125**Return value** 3126 3127| Type | Description | 3128| ------------------- | ------------------------- | 3129| Promise<TaskInfo> | Promise Promise used to return task details.| 3130 3131**Error codes** 3132For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3133 3134 | ID| Error Message| 3135 | -------- | -------- | 3136 | 13400003 | task service ability error. | 3137 | 21900006 | task not found error. | 3138 3139**Example** 3140 3141 ```js 3142 request.agent.show("123456").then((TaskInfo) => { 3143 console.info(`Succeeded in showing a unload task.`); 3144 }).catch((err) => { 3145 console.error(`Failed to show a unload task, Code: ${err.code}, message: ${err.message}`); 3146 }); 3147 ``` 3148 3149 3150## request.agent.touch<sup>10+</sup> 3151 3152touch(id: string, token: string, callback: AsyncCallback<TaskInfo>): void 3153 3154Queries the task details based on the task ID and token. This API uses an asynchronous callback to return the result. 3155 3156**System capability**: SystemCapability.Request.FileTransferAgent 3157 3158**Parameters** 3159 3160 | Name| Type| Mandatory| Description| 3161 | -------- | -------- | -------- | -------- | 3162 | id | string | Yes| Task ID.| 3163 | token | string | Yes| Token for task query.| 3164 | callback | AsyncCallback<void> | Yes| Callback used to return task details.| 3165 3166**Error codes** 3167For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3168 3169 | ID| Error Message| 3170 | -------- | -------- | 3171 | 13400003 | task service ability error. | 3172 | 21900006 | task not found error. | 3173 3174**Example** 3175 3176 ```js 3177 request.agent.touch("123456", "token", (err, TaskInfo) => { 3178 if (err) { 3179 console.error(`Failed to touch a unload task, Code: ${err.code}, message: ${err.message}`); 3180 return; 3181 } 3182 console.info(`Succeeded in touching a unload task.`); 3183 }); 3184 ``` 3185 3186 3187## request.agent.touch<sup>10+</sup> 3188 3189touch(id: string, token: string): Promise<TaskInfo> 3190 3191Queries the task details based on the task ID and token. This API uses a promise to return the result. 3192 3193**System capability**: SystemCapability.Request.FileTransferAgent 3194 3195**Parameters** 3196 3197 | Name| Type| Mandatory| Description| 3198 | -------- | -------- | -------- | -------- | 3199 | id | string | Yes| Task ID.| 3200 | token | string | Yes| Token for task query.| 3201 3202**Return value** 3203 3204| Type | Description | 3205| ------------------- | ------------------------- | 3206| Promise<TaskInfo> | Promise Promise used to return task details.| 3207 3208**Error codes** 3209For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3210 3211 | ID| Error Message| 3212 | -------- | -------- | 3213 | 13400003 | task service ability error. | 3214 | 21900006 | task not found error. | 3215 3216**Example** 3217 3218 ```js 3219 request.agent.touch("123456", "token").then((TaskInfo) => { 3220 console.info(`Succeeded in touching a unload task. `); 3221 }).catch((err) => { 3222 console.error(`Failed to touch a unload task, Code: ${err.code}, message: ${err.message}`); 3223 }); 3224 ``` 3225 3226 3227## request.agent.search<sup>10+</sup> 3228 3229search(filter: Filter, callback: AsyncCallback<Array<string>>): void 3230 3231Searches for task IDs based on [Filter](#filter10). This API uses an asynchronous callback to return the result. 3232 3233**System capability**: SystemCapability.Request.FileTransferAgent 3234 3235**Parameters** 3236 3237 | Name| Type| Mandatory| Description| 3238 | -------- | -------- | -------- | -------- | 3239 | filter | [Filter](#filter10) | Yes| Filter criteria.| 3240 | callback | AsyncCallback<Array<string>> | Yes| Callback used to return task ID matches.| 3241 3242**Error codes** 3243For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3244 3245 | ID| Error Message| 3246 | -------- | -------- | 3247 | 13400003 | task service ability error. | 3248 3249**Example** 3250 3251 ```js 3252 let filter ={ 3253 bundle:"com.example.myapplication", 3254 action: request.agent.Action.UPLOAD, 3255 mode:request.agent.Mode.BACKGROUND 3256 } 3257 request.agent.search(filter, (err, data) => { 3258 if (err) { 3259 console.error(`Failed to search a unload task, Code: ${err.code}, message: ${err.message}`); 3260 return; 3261 } 3262 console.info(`Succeeded in searching a unload task. `); 3263 }); 3264 ``` 3265 3266 3267## request.agent.search<sup>10+</sup> 3268 3269search(filter: Filter): Promise<Array<string>> 3270 3271Searches for task IDs based on [Filter](#filter10). This API uses a promise to return the result. 3272 3273**System capability**: SystemCapability.Request.FileTransferAgent 3274 3275**Parameters** 3276 3277 | Name| Type| Mandatory| Description| 3278 | -------- | -------- | -------- | -------- | 3279 | filter | [Filter](#filter10) | Yes| Filter criteria.| 3280 3281**Return value** 3282 3283| Type | Description | 3284| ------------------- | ------------------------- | 3285| Promise<Array<string>> | Promise Promise used to return task ID matches.| 3286 3287**Error codes** 3288For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3289 3290 | ID| Error Message| 3291 | -------- | -------- | 3292 | 13400003 | task service ability error. | 3293 3294**Example** 3295 3296 ```js 3297 let filter ={ 3298 bundle:"com.example.myapplication", 3299 action: request.agent.Action.UPLOAD, 3300 mode: request.agent.Mode.BACKGROUND 3301 } 3302 request.agent.search(filter).then((data) => { 3303 console.info(`Succeeded in searching a unload task. `); 3304 }).catch((err) => { 3305 console.error(`Failed to search a unload task, Code: ${err.code}, message: ${err.message}`); 3306 }); 3307 ``` 3308 3309 3310## request.agent.query<sup>10+</sup> 3311 3312query(id: string, callback: AsyncCallback<TaskInfo>): void 3313 3314Queries the task details based on the task ID. This API uses an asynchronous callback to return the result. 3315 3316**Required permissions**: ohos.permission.DOWNLOAD_SESSION_MANAGER or ohos.permission.UPLOAD_SESSION_MANAGER 3317 3318**System capability**: SystemCapability.Request.FileTransferAgent 3319 3320**System API**: This is a system API. 3321 3322**Parameters** 3323 3324 | Name| Type| Mandatory| Description| 3325 | -------- | -------- | -------- | -------- | 3326 | id | string | Yes| Task ID.| 3327 | callback | AsyncCallback<TaskInfo> | Yes| Callback used to return task details.| 3328 3329**Error codes** 3330For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3331 3332 | ID| Error Message| 3333 | -------- | -------- | 3334 | 13400003 | task service ability error. | 3335 | 21900006 | task not found error. | 3336 3337**Example** 3338 3339 ```js 3340 request.agent.query("123456", (err, TaskInfo) => { 3341 if (err) { 3342 console.error(`Failed to query a unload task, Code: ${err.code}, message: ${err.message}`); 3343 return; 3344 } 3345 console.info(`Succeeded in querying a unload task. result: ${TaskInfo.uid}`); 3346 }); 3347 ``` 3348 3349 3350## request.agent.query<sup>10+</sup> 3351 3352query(id: string): Promise<TaskInfo> 3353 3354Queries the task details based on the task ID. This API uses a promise to return the result. 3355 3356**Required permissions**: ohos.permission.DOWNLOAD_SESSION_MANAGER or ohos.permission.UPLOAD_SESSION_MANAGER 3357 3358**System capability**: SystemCapability.Request.FileTransferAgent 3359 3360**System API**: This is a system API. 3361 3362**Parameters** 3363 3364 | Name| Type| Mandatory| Description| 3365 | -------- | -------- | -------- | -------- | 3366 | id | string | Yes| Task ID.| 3367 3368**Return value** 3369 3370| Type | Description | 3371| ------------------- | ------------------------- | 3372| Promise<TaskInfo> | Promise Promise used to return task details.| 3373 3374**Error codes** 3375For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md). 3376 3377 | ID| Error Message| 3378 | -------- | -------- | 3379 | 13400003 | task service ability error. | 3380 | 21900006 | task not found error. | 3381 3382**Example** 3383 3384 ```js 3385 request.agent.query("123456",).then((TaskInfo) => { 3386 console.info(`Succeeded in querying a unload task. result: ${TaskInfo.uid}`); 3387 }).catch((err) => { 3388 console.error(`Failed to query a unload task, Code: ${err.code}, message: ${err.message}`); 3389 }); 3390 ``` 3391 3392<!--no_check--> 3393