• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (WebDownloadManager)
2
3Implements a **WebDownloadManager** class. You can use the APIs of this class to resume failed download tasks.
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## Modules to Import
14
15```ts
16import { webview } from '@kit.ArkWeb';
17```
18
19## setDownloadDelegate<sup>11+</sup>
20
21static setDownloadDelegate(delegate: WebDownloadDelegate): void
22
23Sets the delegate used to receive download progress triggered by **WebDownloadManager**.
24
25**System capability**: SystemCapability.Web.Webview.Core
26
27**Parameters**
28
29| Name         | Type   |  Mandatory | Description                                           |
30| ---------------| ------- | ---- | ------------- |
31| delegate      | [WebDownloadDelegate](./arkts-apis-webview-WebDownloadDelegate.md)  | Yes  | Delegate used to receive the download progress.|
32
33**Example**
34
35```ts
36// xxx.ets
37import { webview } from '@kit.ArkWeb';
38import { BusinessError } from '@kit.BasicServicesKit';
39
40@Entry
41@Component
42struct WebComponent {
43  controller: webview.WebviewController = new webview.WebviewController();
44  delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate();
45  download: webview.WebDownloadItem = new webview.WebDownloadItem();
46  failedData: Uint8Array = new Uint8Array();
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 percent complete: " + webDownloadItem.getPercentComplete());
60              this.download = webDownloadItem;
61            })
62            this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => {
63              console.log("download failed guid: " + webDownloadItem.getGuid());
64              // Serialize the failed download to a byte array.
65              this.failedData = webDownloadItem.serialize();
66            })
67            this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => {
68              console.log("download finish guid: " + webDownloadItem.getGuid());
69            })
70            this.controller.setDownloadDelegate(this.delegate);
71            webview.WebDownloadManager.setDownloadDelegate(this.delegate);
72          } catch (error) {
73            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
74          }
75        })
76      Button('startDownload')
77        .onClick(() => {
78          try {
79            this.controller.startDownload('https://www.example.com');
80          } catch (error) {
81            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
82          }
83        })
84      Button('resumeDownload')
85        .onClick(() => {
86          try {
87            webview.WebDownloadManager.resumeDownload(webview.WebDownloadItem.deserialize(this.failedData));
88          } catch (error) {
89            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
90          }
91        })
92      Button('cancel')
93        .onClick(() => {
94          try {
95            this.download.cancel();
96          } catch (error) {
97            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
98          }
99        })
100      Button('pause')
101        .onClick(() => {
102          try {
103            this.download.pause();
104          } catch (error) {
105            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
106          }
107        })
108      Button('resume')
109        .onClick(() => {
110          try {
111            this.download.resume();
112          } catch (error) {
113            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
114          }
115        })
116      Web({ src: 'www.example.com', controller: this.controller })
117    }
118  }
119}
120```
121
122## resumeDownload<sup>11+</sup>
123
124static resumeDownload(webDownloadItem: WebDownloadItem): void
125
126Resumes a failed download task.
127
128**System capability**: SystemCapability.Web.Webview.Core
129
130**Parameters**
131
132| Name         | Type   |  Mandatory | Description                                           |
133| ---------------| ------- | ---- | ------------- |
134| webDownloadItem      | [WebDownloadItem](./arkts-apis-webview-WebDownloadItem.md)  | Yes  | Download task to resume.|
135
136**Error codes**
137
138For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
139
140| Error Code| Error Message                             |
141| -------- | ------------------------------------- |
142| 17100018 | No WebDownloadDelegate has been set yet. |
143
144**Example**
145
146```ts
147// xxx.ets
148import { webview } from '@kit.ArkWeb';
149import { BusinessError } from '@kit.BasicServicesKit';
150
151@Entry
152@Component
153struct WebComponent {
154  controller: webview.WebviewController = new webview.WebviewController();
155  delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate();
156  download: webview.WebDownloadItem = new webview.WebDownloadItem();
157  failedData: Uint8Array = new Uint8Array();
158
159  build() {
160    Column() {
161      Button('setDownloadDelegate')
162        .onClick(() => {
163          try {
164            this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => {
165              console.log("will start a download.");
166              // Pass in a download path and start the download.
167              webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName());
168            })
169            this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => {
170              console.log("download update percent complete: " + webDownloadItem.getPercentComplete());
171              this.download = webDownloadItem;
172            })
173            this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => {
174              console.log("download failed guid: " + webDownloadItem.getGuid());
175              // Serialize the failed download to a byte array.
176              this.failedData = webDownloadItem.serialize();
177            })
178            this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => {
179              console.log("download finish guid: " + webDownloadItem.getGuid());
180            })
181            this.controller.setDownloadDelegate(this.delegate);
182            webview.WebDownloadManager.setDownloadDelegate(this.delegate);
183          } catch (error) {
184            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
185          }
186        })
187      Button('startDownload')
188        .onClick(() => {
189          try {
190            this.controller.startDownload('https://www.example.com');
191          } catch (error) {
192            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
193          }
194        })
195      Button('resumeDownload')
196        .onClick(() => {
197          try {
198            webview.WebDownloadManager.resumeDownload(webview.WebDownloadItem.deserialize(this.failedData));
199          } catch (error) {
200            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
201          }
202        })
203      Button('cancel')
204        .onClick(() => {
205          try {
206            this.download.cancel();
207          } catch (error) {
208            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
209          }
210        })
211      Button('pause')
212        .onClick(() => {
213          try {
214            this.download.pause();
215          } catch (error) {
216            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
217          }
218        })
219      Button('resume')
220        .onClick(() => {
221          try {
222            this.download.resume();
223          } catch (error) {
224            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
225          }
226        })
227      Web({ src: 'www.example.com', controller: this.controller })
228    }
229  }
230}
231```
232