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