1# Class (WebDownloadItem) 2 3 Implements a **WebDownloadItem** object. You can use this object to perform operations on the corresponding download task. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version. 8> 9> - The initial APIs of this class are supported since API version 11. 10> 11> - You can preview how this component looks on a real device, but not in DevEco Studio Previewer. 12> 13> - During the download, the download process is notified to the user through **WebDownloadDelegate**. The user can operate the download task through the **WebDownloadItem** parameter. 14 15## Modules to Import 16 17```ts 18import { webview } from '@kit.ArkWeb'; 19``` 20 21## getGuid<sup>11+</sup> 22 23getGuid(): string 24 25Obtains the unique ID of this download task. 26 27**System capability**: SystemCapability.Web.Webview.Core 28 29**Return value** 30 31| Type | Description | 32| ------ | ------------------------- | 33| string | Unique ID of the download task.| 34 35**Example** 36 37```ts 38// xxx.ets 39import { webview } from '@kit.ArkWeb'; 40import { BusinessError } from '@kit.BasicServicesKit'; 41 42@Entry 43@Component 44struct WebComponent { 45 controller: webview.WebviewController = new webview.WebviewController(); 46 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 47 48 build() { 49 Column() { 50 Button('setDownloadDelegate') 51 .onClick(() => { 52 try { 53 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 54 console.log("will start a download."); 55 // Pass in a download path and start the download. 56 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 57 }) 58 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 59 console.log("download update guid: " + webDownloadItem.getGuid()); 60 }) 61 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 62 console.log("download failed guid: " + webDownloadItem.getGuid()); 63 }) 64 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 65 console.log("download finish guid: " + webDownloadItem.getGuid()); 66 }) 67 this.controller.setDownloadDelegate(this.delegate); 68 } catch (error) { 69 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 70 } 71 }) 72 Button('startDownload') 73 .onClick(() => { 74 try { 75 this.controller.startDownload('https://www.example.com'); 76 } catch (error) { 77 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 78 } 79 }) 80 Web({ src: 'www.example.com', controller: this.controller }) 81 } 82 } 83} 84``` 85 86## getCurrentSpeed<sup>11+</sup> 87 88getCurrentSpeed(): number 89 90Obtains the download speed, in bytes per second. 91 92**System capability**: SystemCapability.Web.Webview.Core 93 94**Return value** 95 96| Type | Description | 97| ------ | ------------------------- | 98| number | Download speed, in bytes per second.| 99 100**Example** 101 102```ts 103// xxx.ets 104import { webview } from '@kit.ArkWeb'; 105import { BusinessError } from '@kit.BasicServicesKit'; 106 107@Entry 108@Component 109struct WebComponent { 110 controller: webview.WebviewController = new webview.WebviewController(); 111 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 112 113 build() { 114 Column() { 115 Button('setDownloadDelegate') 116 .onClick(() => { 117 try { 118 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 119 console.log("will start a download."); 120 // Pass in a download path and start the download. 121 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 122 }) 123 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 124 console.log("download update current speed: " + webDownloadItem.getCurrentSpeed()); 125 }) 126 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 127 console.log("download failed guid: " + webDownloadItem.getGuid()); 128 }) 129 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 130 console.log("download finish guid: " + webDownloadItem.getGuid()); 131 }) 132 this.controller.setDownloadDelegate(this.delegate); 133 } catch (error) { 134 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 135 } 136 }) 137 Button('startDownload') 138 .onClick(() => { 139 try { 140 this.controller.startDownload('https://www.example.com'); 141 } catch (error) { 142 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 143 } 144 }) 145 Web({ src: 'www.example.com', controller: this.controller }) 146 } 147 } 148} 149``` 150 151## getPercentComplete<sup>11+</sup> 152 153getPercentComplete(): number 154 155Obtains the download progress. The value **100** indicates that the download is complete. 156 157**System capability**: SystemCapability.Web.Webview.Core 158 159**Return value** 160 161| Type | Description | 162| ------ | ------------------------- | 163| number | Download progress. The value **100** indicates that the download is complete.| 164 165**Example** 166 167```ts 168// xxx.ets 169import { webview } from '@kit.ArkWeb'; 170import { BusinessError } from '@kit.BasicServicesKit'; 171 172@Entry 173@Component 174struct WebComponent { 175 controller: webview.WebviewController = new webview.WebviewController(); 176 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 177 178 build() { 179 Column() { 180 Button('setDownloadDelegate') 181 .onClick(() => { 182 try { 183 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 184 console.log("will start a download."); 185 // Pass in a download path and start the download. 186 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 187 }) 188 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 189 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 190 }) 191 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 192 console.log("download failed guid: " + webDownloadItem.getGuid()); 193 }) 194 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 195 console.log("download finish guid: " + webDownloadItem.getGuid()); 196 }) 197 this.controller.setDownloadDelegate(this.delegate); 198 } catch (error) { 199 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 200 } 201 }) 202 Button('startDownload') 203 .onClick(() => { 204 try { 205 this.controller.startDownload('https://www.example.com'); 206 } catch (error) { 207 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 208 } 209 }) 210 Web({ src: 'www.example.com', controller: this.controller }) 211 } 212 } 213} 214``` 215 216## getTotalBytes<sup>11+</sup> 217 218getTotalBytes(): number 219 220Obtains the total length of the file to be downloaded. 221 222**System capability**: SystemCapability.Web.Webview.Core 223 224**Return value** 225 226| Type | Description | 227| ------ | ------------------------- | 228| number | Total length of the file to be downloaded.| 229 230**Example** 231 232```ts 233// xxx.ets 234import { webview } from '@kit.ArkWeb'; 235import { BusinessError } from '@kit.BasicServicesKit'; 236 237@Entry 238@Component 239struct WebComponent { 240 controller: webview.WebviewController = new webview.WebviewController(); 241 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 242 243 build() { 244 Column() { 245 Button('setDownloadDelegate') 246 .onClick(() => { 247 try { 248 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 249 console.log("will start a download."); 250 // Pass in a download path and start the download. 251 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 252 }) 253 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 254 console.log("download update total bytes: " + webDownloadItem.getTotalBytes()); 255 }) 256 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 257 console.log("download failed guid: " + webDownloadItem.getGuid()); 258 }) 259 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 260 console.log("download finish guid: " + webDownloadItem.getGuid()); 261 }) 262 this.controller.setDownloadDelegate(this.delegate); 263 } catch (error) { 264 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 265 } 266 }) 267 Button('startDownload') 268 .onClick(() => { 269 try { 270 this.controller.startDownload('https://www.example.com'); 271 } catch (error) { 272 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 273 } 274 }) 275 Web({ src: 'www.example.com', controller: this.controller }) 276 } 277 } 278} 279``` 280 281## getState<sup>11+</sup> 282 283getState(): WebDownloadState 284 285Obtains the download state. 286 287**System capability**: SystemCapability.Web.Webview.Core 288 289**Return value** 290 291| Type | Description | 292| ------ | ------------------------- | 293| [WebDownloadState](./arkts-apis-webview-e.md#webdownloadstate11) | Download state.| 294 295**Example** 296 297```ts 298// xxx.ets 299import { webview } from '@kit.ArkWeb'; 300import { BusinessError } from '@kit.BasicServicesKit'; 301 302@Entry 303@Component 304struct WebComponent { 305 controller: webview.WebviewController = new webview.WebviewController(); 306 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 307 308 build() { 309 Column() { 310 Button('setDownloadDelegate') 311 .onClick(() => { 312 try { 313 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 314 console.log("will start a download."); 315 // Pass in a download path and start the download. 316 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 317 }) 318 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 319 console.log("download update download state: " + webDownloadItem.getState()); 320 }) 321 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 322 console.log("download failed guid: " + webDownloadItem.getGuid()); 323 }) 324 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 325 console.log("download finish guid: " + webDownloadItem.getGuid()); 326 }) 327 this.controller.setDownloadDelegate(this.delegate); 328 } catch (error) { 329 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 330 } 331 }) 332 Button('startDownload') 333 .onClick(() => { 334 try { 335 this.controller.startDownload('https://www.example.com'); 336 } catch (error) { 337 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 338 } 339 }) 340 Web({ src: 'www.example.com', controller: this.controller }) 341 } 342 } 343} 344``` 345 346## getLastErrorCode<sup>11+</sup> 347 348getLastErrorCode(): WebDownloadErrorCode 349 350Obtains the download error code. 351 352**System capability**: SystemCapability.Web.Webview.Core 353 354**Return value** 355 356| Type | Description | 357| ------ | ------------------------- | 358| [WebDownloadErrorCode](./arkts-apis-webview-e.md#webdownloaderrorcode11) | Error code returned when the download error occurs.| 359 360**Example** 361 362```ts 363// xxx.ets 364import { webview } from '@kit.ArkWeb'; 365import { BusinessError } from '@kit.BasicServicesKit'; 366 367@Entry 368@Component 369struct WebComponent { 370 controller: webview.WebviewController = new webview.WebviewController(); 371 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 372 373 build() { 374 Column() { 375 Button('setDownloadDelegate') 376 .onClick(() => { 377 try { 378 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 379 console.log("will start a download."); 380 // Pass in a download path and start the download. 381 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 382 }) 383 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 384 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 385 }) 386 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 387 console.log("download failed guid: " + webDownloadItem.getGuid()); 388 console.log("download error code: " + webDownloadItem.getLastErrorCode()); 389 }) 390 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 391 console.log("download finish guid: " + webDownloadItem.getGuid()); 392 }) 393 this.controller.setDownloadDelegate(this.delegate); 394 } catch (error) { 395 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 396 } 397 }) 398 Button('startDownload') 399 .onClick(() => { 400 try { 401 this.controller.startDownload('https://www.example.com'); 402 } catch (error) { 403 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 404 } 405 }) 406 Web({ src: 'www.example.com', controller: this.controller }) 407 } 408 } 409} 410``` 411 412## getMethod<sup>11+</sup> 413 414getMethod(): string 415 416Obtains the request mode of this download task. 417 418**System capability**: SystemCapability.Web.Webview.Core 419 420**Return value** 421 422| Type | Description | 423| ------ | ------------------------- | 424| string | Request mode of the download task.| 425 426**Example** 427 428```ts 429// xxx.ets 430import { webview } from '@kit.ArkWeb'; 431import { BusinessError } from '@kit.BasicServicesKit'; 432 433@Entry 434@Component 435struct WebComponent { 436 controller: webview.WebviewController = new webview.WebviewController(); 437 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 438 439 build() { 440 Column() { 441 Button('setDownloadDelegate') 442 .onClick(() => { 443 try { 444 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 445 console.log("Download will start. Method:" + webDownloadItem.getMethod()); 446 // Pass in a download path and start the download. 447 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 448 }) 449 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 450 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 451 }) 452 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 453 console.log("download failed guid: " + webDownloadItem.getGuid()); 454 }) 455 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 456 console.log("download finish guid: " + webDownloadItem.getGuid()); 457 }) 458 this.controller.setDownloadDelegate(this.delegate); 459 } catch (error) { 460 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 461 } 462 }) 463 Button('startDownload') 464 .onClick(() => { 465 try { 466 this.controller.startDownload('https://www.example.com'); 467 } catch (error) { 468 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 469 } 470 }) 471 Web({ src: 'www.example.com', controller: this.controller }) 472 } 473 } 474} 475``` 476 477## getMimeType<sup>11+</sup> 478 479getMimeType(): string 480 481Obtains the MIME type of this download task (for example, a sound file may be marked as audio/ogg, and an image file may be image/png). 482 483**System capability**: SystemCapability.Web.Webview.Core 484 485**Return value** 486 487| Type | Description | 488| ------ | ------------------------- | 489| string | MIME type (for example, audio/ogg for a sound file, and image/png for an image file).| 490 491**Example** 492 493```ts 494// xxx.ets 495import { webview } from '@kit.ArkWeb'; 496import { BusinessError } from '@kit.BasicServicesKit'; 497 498@Entry 499@Component 500struct WebComponent { 501 controller: webview.WebviewController = new webview.WebviewController(); 502 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 503 504 build() { 505 Column() { 506 Button('setDownloadDelegate') 507 .onClick(() => { 508 try { 509 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 510 console.log("Download will start. MIME type:" + webDownloadItem.getMimeType()); 511 // Pass in a download path and start the download. 512 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 513 }) 514 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 515 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 516 }) 517 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 518 console.log("download failed guid: " + webDownloadItem.getGuid()); 519 }) 520 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 521 console.log("download finish guid: " + webDownloadItem.getGuid()); 522 }) 523 this.controller.setDownloadDelegate(this.delegate); 524 } catch (error) { 525 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 526 } 527 }) 528 Button('startDownload') 529 .onClick(() => { 530 try { 531 this.controller.startDownload('https://www.example.com'); 532 } catch (error) { 533 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 534 } 535 }) 536 Web({ src: 'www.example.com', controller: this.controller }) 537 } 538 } 539} 540``` 541 542## getUrl<sup>11+</sup> 543 544getUrl(): string 545 546Obtains the download request URL. 547 548**System capability**: SystemCapability.Web.Webview.Core 549 550**Return value** 551 552| Type | Description | 553| ------ | ------------------------- | 554| string | Download request URL.| 555 556**Example** 557 558```ts 559// xxx.ets 560import { webview } from '@kit.ArkWeb'; 561import { BusinessError } from '@kit.BasicServicesKit'; 562 563@Entry 564@Component 565struct WebComponent { 566 controller: webview.WebviewController = new webview.WebviewController(); 567 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 568 569 build() { 570 Column() { 571 Button('setDownloadDelegate') 572 .onClick(() => { 573 try { 574 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 575 console.log("will start a download, url:" + webDownloadItem.getUrl()); 576 // Pass in a download path and start the download. 577 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 578 }) 579 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 580 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 581 }) 582 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 583 console.log("download failed guid: " + webDownloadItem.getGuid()); 584 }) 585 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 586 console.log("download finish guid: " + webDownloadItem.getGuid()); 587 }) 588 this.controller.setDownloadDelegate(this.delegate); 589 } catch (error) { 590 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 591 } 592 }) 593 Button('startDownload') 594 .onClick(() => { 595 try { 596 this.controller.startDownload('https://www.example.com'); 597 } catch (error) { 598 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 599 } 600 }) 601 Web({ src: 'www.example.com', controller: this.controller }) 602 } 603 } 604} 605``` 606 607## getSuggestedFileName<sup>11+</sup> 608 609getSuggestedFileName(): string 610 611Obtains the suggested file name for this download task. 612 613**System capability**: SystemCapability.Web.Webview.Core 614 615**Return value** 616 617| Type | Description | 618| ------ | ------------------------- | 619| string | Suggested file name.| 620 621**Example** 622 623```ts 624// xxx.ets 625import { webview } from '@kit.ArkWeb'; 626import { BusinessError } from '@kit.BasicServicesKit'; 627 628@Entry 629@Component 630struct WebComponent { 631 controller: webview.WebviewController = new webview.WebviewController(); 632 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 633 634 build() { 635 Column() { 636 Button('setDownloadDelegate') 637 .onClick(() => { 638 try { 639 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 640 console.log("will start a download, suggest name:" + webDownloadItem.getSuggestedFileName()); 641 // Pass in a download path and start the download. 642 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 643 }) 644 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 645 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 646 }) 647 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 648 console.log("download failed guid: " + webDownloadItem.getGuid()); 649 }) 650 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 651 console.log("download finish guid: " + webDownloadItem.getGuid()); 652 }) 653 this.controller.setDownloadDelegate(this.delegate); 654 } catch (error) { 655 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 656 } 657 }) 658 Button('startDownload') 659 .onClick(() => { 660 try { 661 this.controller.startDownload('https://www.example.com'); 662 } catch (error) { 663 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 664 } 665 }) 666 Web({ src: 'www.example.com', controller: this.controller }) 667 } 668 } 669} 670``` 671 672## getReceivedBytes<sup>11+</sup> 673 674getReceivedBytes(): number 675 676Obtains the number of received bytes. 677 678**System capability**: SystemCapability.Web.Webview.Core 679 680**Return value** 681 682| Type | Description | 683| ------ | ------------------------- | 684| number | Number of received bytes.| 685 686**Example** 687 688```ts 689// xxx.ets 690import { webview } from '@kit.ArkWeb'; 691import { BusinessError } from '@kit.BasicServicesKit'; 692 693@Entry 694@Component 695struct WebComponent { 696 controller: webview.WebviewController = new webview.WebviewController(); 697 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 698 699 build() { 700 Column() { 701 Button('setDownloadDelegate') 702 .onClick(() => { 703 try { 704 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 705 console.log("will start a download."); 706 // Pass in a download path and start the download. 707 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 708 }) 709 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 710 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 711 console.log("download update received bytes: " + webDownloadItem.getReceivedBytes()); 712 }) 713 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 714 console.log("download failed guid: " + webDownloadItem.getGuid()); 715 }) 716 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 717 console.log("download finish guid: " + webDownloadItem.getGuid()); 718 }) 719 this.controller.setDownloadDelegate(this.delegate); 720 } catch (error) { 721 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 722 } 723 }) 724 Button('startDownload') 725 .onClick(() => { 726 try { 727 this.controller.startDownload('https://www.example.com'); 728 } catch (error) { 729 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 730 } 731 }) 732 Web({ src: 'www.example.com', controller: this.controller }) 733 } 734 } 735} 736``` 737 738## getFullPath<sup>11+</sup> 739 740getFullPath(): string 741 742Obtains the full path of the downloaded file on the disk. 743 744**System capability**: SystemCapability.Web.Webview.Core 745 746**Return value** 747 748| Type | Description | 749| ------ | ------------------------- | 750| string | Full path of the downloaded file on the disk.| 751 752**Example** 753 754```ts 755// xxx.ets 756import { webview } from '@kit.ArkWeb'; 757import { BusinessError } from '@kit.BasicServicesKit'; 758 759@Entry 760@Component 761struct WebComponent { 762 controller: webview.WebviewController = new webview.WebviewController(); 763 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 764 765 build() { 766 Column() { 767 Button('setDownloadDelegate') 768 .onClick(() => { 769 try { 770 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 771 console.log("will start a download."); 772 // Pass in a download path and start the download. 773 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 774 }) 775 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 776 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 777 }) 778 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 779 console.log("download failed guid: " + webDownloadItem.getGuid()); 780 }) 781 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 782 console.log("download finish guid: " + webDownloadItem.getGuid()); 783 console.log("download finish full path: " + webDownloadItem.getFullPath()); 784 }) 785 this.controller.setDownloadDelegate(this.delegate); 786 } catch (error) { 787 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 788 } 789 }) 790 Button('startDownload') 791 .onClick(() => { 792 try { 793 this.controller.startDownload('https://www.example.com'); 794 } catch (error) { 795 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 796 } 797 }) 798 Web({ src: 'www.example.com', controller: this.controller }) 799 } 800 } 801} 802``` 803 804## serialize<sup>11+</sup> 805 806serialize(): Uint8Array 807 808Serializes the failed download to a byte array. 809 810**System capability**: SystemCapability.Web.Webview.Core 811 812**Return value** 813 814| Type | Description | 815| ------ | ------------------------- | 816| Uint8Array | Byte array into which the failed download is serialized.| 817 818**Example** 819 820```ts 821// xxx.ets 822import { webview } from '@kit.ArkWeb'; 823import { BusinessError } from '@kit.BasicServicesKit'; 824 825@Entry 826@Component 827struct WebComponent { 828 controller: webview.WebviewController = new webview.WebviewController(); 829 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 830 failedData: Uint8Array = new Uint8Array(); 831 832 build() { 833 Column() { 834 Button('setDownloadDelegate') 835 .onClick(() => { 836 try { 837 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 838 console.log("will start a download."); 839 // Pass in a download path and start the download. 840 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 841 }) 842 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 843 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 844 }) 845 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 846 console.log("download failed guid: " + webDownloadItem.getGuid()); 847 // Serialize the failed download to a byte array. 848 this.failedData = webDownloadItem.serialize(); 849 }) 850 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 851 console.log("download finish guid: " + webDownloadItem.getGuid()); 852 }) 853 this.controller.setDownloadDelegate(this.delegate); 854 } catch (error) { 855 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 856 } 857 }) 858 Button('startDownload') 859 .onClick(() => { 860 try { 861 this.controller.startDownload('https://www.example.com'); 862 } catch (error) { 863 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 864 } 865 }) 866 Web({ src: 'www.example.com', controller: this.controller }) 867 } 868 } 869} 870``` 871 872## deserialize<sup>11+</sup> 873 874static deserialize(serializedData: Uint8Array): WebDownloadItem 875 876Deserializes the serialized byte array into a **WebDownloadItem** object. 877 878**System capability**: SystemCapability.Web.Webview.Core 879 880**Parameters** 881 882| Name | Type | Mandatory | Description| 883| ------------------ | ------- | ---- | ------------- | 884| serializedData | Uint8Array | Yes | Byte array into which the download is serialized.| 885 886**Return value** 887 888| Type | Description | 889| ------ | ------------------------- | 890| WebDownloadItem | **WebDownloadItem** object.| 891 892**Error codes** 893 894For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 895 896| ID| Error Message | 897| -------- | ------------------------------------------------------------ | 898| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. 2. Parameter verification failed. | 899 900**Example** 901 902```ts 903// xxx.ets 904import { webview } from '@kit.ArkWeb'; 905import { BusinessError } from '@kit.BasicServicesKit'; 906 907@Entry 908@Component 909struct WebComponent { 910 controller: webview.WebviewController = new webview.WebviewController(); 911 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 912 failedData: Uint8Array = new Uint8Array(); 913 914 build() { 915 Column() { 916 Button('setDownloadDelegate') 917 .onClick(() => { 918 try { 919 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 920 console.log("will start a download."); 921 // Pass in a download path and start the download. 922 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 923 }) 924 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 925 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 926 }) 927 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 928 console.log("download failed guid: " + webDownloadItem.getGuid()); 929 // Serialize the failed download to a byte array. 930 this.failedData = webDownloadItem.serialize(); 931 }) 932 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 933 console.log("download finish guid: " + webDownloadItem.getGuid()); 934 }) 935 this.controller.setDownloadDelegate(this.delegate); 936 } catch (error) { 937 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 938 } 939 }) 940 Button('startDownload') 941 .onClick(() => { 942 try { 943 this.controller.startDownload('https://www.example.com'); 944 } catch (error) { 945 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 946 } 947 }) 948 Button('resumeDownload') 949 .onClick(() => { 950 try { 951 webview.WebDownloadManager.resumeDownload(webview.WebDownloadItem.deserialize(this.failedData)); 952 } catch (error) { 953 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 954 } 955 }) 956 Web({ src: 'www.example.com', controller: this.controller }) 957 } 958 } 959} 960``` 961 962## start<sup>11+</sup> 963 964start(downloadPath: string): void 965 966Starts a download to a specified directory. 967 968> **NOTE** 969> 970>This API must be used in the **onBeforeDownload** callback of **WebDownloadDelegate**. If it is not called in the callback, the download task remains in the PENDING state and is downloaded to a temporary directory. After the target path is specified by **WebDownloadItem.start**, the temporary files are renamed to the target path and the unfinished files are directly downloaded to the target path. If you do not want to download the file to the temporary directory before invoking **WebDownloadItem.start**, you can call **WebDownloadItem.cancel** to cancel the current download task and then call **WebDownloadManager.resumeDownload** to resume the task. 971 972**System capability**: SystemCapability.Web.Webview.Core 973 974**Parameters** 975 976| Name| Type | Mandatory| Description | 977| ------ | ---------------------- | ---- | ------------------------------| 978| downloadPath | string | Yes | Path (including the file name) for downloading the file. The path length is the same as that in file management and can contain a maximum of 255 characters.| 979 980**Error codes** 981 982For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 983 984| ID| Error Message | 985| -------- | ------------------------------------------------------------ | 986| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. 2. Parameter verification failed. | 987 988**Example** 989 990```ts 991// xxx.ets 992import { webview } from '@kit.ArkWeb'; 993import { BusinessError } from '@kit.BasicServicesKit'; 994 995@Entry 996@Component 997struct WebComponent { 998 controller: webview.WebviewController = new webview.WebviewController(); 999 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 1000 failedData: Uint8Array = new Uint8Array(); 1001 1002 build() { 1003 Column() { 1004 Button('setDownloadDelegate') 1005 .onClick(() => { 1006 try { 1007 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 1008 console.log("will start a download."); 1009 // Pass in a download path and start the download. 1010 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 1011 }) 1012 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 1013 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 1014 }) 1015 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 1016 console.log("download failed guid: " + webDownloadItem.getGuid()); 1017 // Serialize the failed download to a byte array. 1018 this.failedData = webDownloadItem.serialize(); 1019 }) 1020 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 1021 console.log("download finish guid: " + webDownloadItem.getGuid()); 1022 }) 1023 this.controller.setDownloadDelegate(this.delegate); 1024 } catch (error) { 1025 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1026 } 1027 }) 1028 Button('startDownload') 1029 .onClick(() => { 1030 try { 1031 this.controller.startDownload('https://www.example.com'); 1032 } catch (error) { 1033 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1034 } 1035 }) 1036 Button('resumeDownload') 1037 .onClick(() => { 1038 try { 1039 webview.WebDownloadManager.resumeDownload(webview.WebDownloadItem.deserialize(this.failedData)); 1040 } catch (error) { 1041 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1042 } 1043 }) 1044 Web({ src: 'www.example.com', controller: this.controller }) 1045 } 1046 } 1047} 1048``` 1049 1050## cancel<sup>11+</sup> 1051 1052cancel(): void 1053 1054Cancels a download task. 1055 1056**System capability**: SystemCapability.Web.Webview.Core 1057 1058**Example** 1059 1060```ts 1061// xxx.ets 1062import { webview } from '@kit.ArkWeb'; 1063import { BusinessError } from '@kit.BasicServicesKit'; 1064 1065@Entry 1066@Component 1067struct WebComponent { 1068 controller: webview.WebviewController = new webview.WebviewController(); 1069 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 1070 download: webview.WebDownloadItem = new webview.WebDownloadItem(); 1071 failedData: Uint8Array = new Uint8Array(); 1072 1073 build() { 1074 Column() { 1075 Button('setDownloadDelegate') 1076 .onClick(() => { 1077 try { 1078 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 1079 console.log("will start a download."); 1080 // Pass in a download path and start the download. 1081 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 1082 }) 1083 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 1084 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 1085 this.download = webDownloadItem; 1086 }) 1087 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 1088 console.log("download failed guid: " + webDownloadItem.getGuid()); 1089 // Serialize the failed download to a byte array. 1090 this.failedData = webDownloadItem.serialize(); 1091 }) 1092 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 1093 console.log("download finish guid: " + webDownloadItem.getGuid()); 1094 }) 1095 this.controller.setDownloadDelegate(this.delegate); 1096 } catch (error) { 1097 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1098 } 1099 }) 1100 Button('startDownload') 1101 .onClick(() => { 1102 try { 1103 this.controller.startDownload('https://www.example.com'); 1104 } catch (error) { 1105 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1106 } 1107 }) 1108 Button('resumeDownload') 1109 .onClick(() => { 1110 try { 1111 webview.WebDownloadManager.resumeDownload(webview.WebDownloadItem.deserialize(this.failedData)); 1112 } catch (error) { 1113 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1114 } 1115 }) 1116 Button('cancel') 1117 .onClick(() => { 1118 try { 1119 this.download.cancel(); 1120 } catch (error) { 1121 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1122 } 1123 }) 1124 Web({ src: 'www.example.com', controller: this.controller }) 1125 } 1126 } 1127} 1128``` 1129 1130## pause<sup>11+</sup> 1131 1132pause(): void 1133 1134Pauses a download task. 1135 1136**System capability**: SystemCapability.Web.Webview.Core 1137 1138**Error codes** 1139 1140For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 1141 1142| ID | Error Message | 1143| -------- | ------------------------------------------------------------ | 1144| 17100019 | The download task is not started yet. | 1145 1146**Example** 1147 1148```ts 1149// xxx.ets 1150import { webview } from '@kit.ArkWeb'; 1151import { BusinessError } from '@kit.BasicServicesKit'; 1152 1153@Entry 1154@Component 1155struct WebComponent { 1156 controller: webview.WebviewController = new webview.WebviewController(); 1157 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 1158 download: webview.WebDownloadItem = new webview.WebDownloadItem(); 1159 failedData: Uint8Array = new Uint8Array(); 1160 1161 build() { 1162 Column() { 1163 Button('setDownloadDelegate') 1164 .onClick(() => { 1165 try { 1166 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 1167 console.log("will start a download."); 1168 // Pass in a download path and start the download. 1169 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 1170 }) 1171 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 1172 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 1173 this.download = webDownloadItem; 1174 }) 1175 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 1176 console.log("download failed guid: " + webDownloadItem.getGuid()); 1177 // Serialize the failed download to a byte array. 1178 this.failedData = webDownloadItem.serialize(); 1179 }) 1180 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 1181 console.log("download finish guid: " + webDownloadItem.getGuid()); 1182 }) 1183 this.controller.setDownloadDelegate(this.delegate); 1184 } catch (error) { 1185 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1186 } 1187 }) 1188 Button('startDownload') 1189 .onClick(() => { 1190 try { 1191 this.controller.startDownload('https://www.example.com'); 1192 } catch (error) { 1193 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1194 } 1195 }) 1196 Button('resumeDownload') 1197 .onClick(() => { 1198 try { 1199 webview.WebDownloadManager.resumeDownload(webview.WebDownloadItem.deserialize(this.failedData)); 1200 } catch (error) { 1201 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1202 } 1203 }) 1204 Button('cancel') 1205 .onClick(() => { 1206 try { 1207 this.download.cancel(); 1208 } catch (error) { 1209 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1210 } 1211 }) 1212 Button('pause') 1213 .onClick(() => { 1214 try { 1215 this.download.pause(); 1216 } catch (error) { 1217 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1218 } 1219 }) 1220 Web({ src: 'www.example.com', controller: this.controller }) 1221 } 1222 } 1223} 1224``` 1225 1226## resume<sup>11+</sup> 1227 1228resume(): void 1229 1230Resumes a download task. 1231 1232**System capability**: SystemCapability.Web.Webview.Core 1233 1234**Error codes** 1235 1236For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 1237 1238| ID | Error Message | 1239| -------- | ------------------------------------------------------------ | 1240| 17100016 | The download task is not paused. | 1241 1242**Example** 1243 1244```ts 1245// xxx.ets 1246import { webview } from '@kit.ArkWeb'; 1247import { BusinessError } from '@kit.BasicServicesKit'; 1248 1249@Entry 1250@Component 1251struct WebComponent { 1252 controller: webview.WebviewController = new webview.WebviewController(); 1253 delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate(); 1254 download: webview.WebDownloadItem = new webview.WebDownloadItem(); 1255 failedData: Uint8Array = new Uint8Array(); 1256 1257 build() { 1258 Column() { 1259 Button('setDownloadDelegate') 1260 .onClick(() => { 1261 try { 1262 this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => { 1263 console.log("will start a download."); 1264 // Pass in a download path and start the download. 1265 webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()); 1266 }) 1267 this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => { 1268 console.log("download update percent complete: " + webDownloadItem.getPercentComplete()); 1269 this.download = webDownloadItem; 1270 }) 1271 this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => { 1272 console.log("download failed guid: " + webDownloadItem.getGuid()); 1273 // Serialize the failed download to a byte array. 1274 this.failedData = webDownloadItem.serialize(); 1275 }) 1276 this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => { 1277 console.log("download finish guid: " + webDownloadItem.getGuid()); 1278 }) 1279 this.controller.setDownloadDelegate(this.delegate); 1280 } catch (error) { 1281 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1282 } 1283 }) 1284 Button('startDownload') 1285 .onClick(() => { 1286 try { 1287 this.controller.startDownload('https://www.example.com'); 1288 } catch (error) { 1289 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1290 } 1291 }) 1292 Button('resumeDownload') 1293 .onClick(() => { 1294 try { 1295 webview.WebDownloadManager.resumeDownload(webview.WebDownloadItem.deserialize(this.failedData)); 1296 } catch (error) { 1297 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1298 } 1299 }) 1300 Button('cancel') 1301 .onClick(() => { 1302 try { 1303 this.download.cancel(); 1304 } catch (error) { 1305 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1306 } 1307 }) 1308 Button('pause') 1309 .onClick(() => { 1310 try { 1311 this.download.pause(); 1312 } catch (error) { 1313 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1314 } 1315 }) 1316 Button('resume') 1317 .onClick(() => { 1318 try { 1319 this.download.resume(); 1320 } catch (error) { 1321 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 1322 } 1323 }) 1324 Web({ src: 'www.example.com', controller: this.controller }) 1325 } 1326 } 1327} 1328``` 1329