• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
18## Constraints
19The download server must support the HTTP HEAD method so that the size of the data to download can be obtained through **Content-length**. Otherwise, the download task fails. If this is the case, you can check the failure cause through [on('fail')<sup>7+</sup>](#onfail7).
20
21Only HTTP requests are supported. HTTPS requests are not supported.
22
23## Constants
24
25**Required permissions**: ohos.permission.INTERNET
26
27**System capability**: SystemCapability.MiscServices.Download
28
29### Network Types
30You can set **networkType** in [DownloadConfig](#downloadconfig) to specify the network type for the download service.
31
32| Name| Type| Value| Description|
33| -------- | -------- | -------- | -------- |
34| NETWORK_MOBILE | number | 0x00000001 | Whether download is allowed on a mobile network.|
35| NETWORK_WIFI | number | 0x00010000 | Whether download is allowed on a WLAN.|
36
37### Download Error Codes
38The table below lists the error codes that may be returned by [on('fail')<sup>7+</sup>](#onfail7)/[off('fail')<sup>7+</sup>](#offfail7)/[getTaskInfo<sup>9+</sup>](#gettaskinfo9).
39
40| Name| Type| Value| Description|
41| -------- | -------- | -------- | -------- |
42| ERROR_CANNOT_RESUME<sup>7+</sup> | number |   0   | Failure to resume the download due to network errors.|
43| ERROR_DEVICE_NOT_FOUND<sup>7+</sup> | number |   1   | Failure to find a storage device such as a memory card.|
44| ERROR_FILE_ALREADY_EXISTS<sup>7+</sup> | number |   2   | Failure to download the file because it already exists.|
45| ERROR_FILE_ERROR<sup>7+</sup> | number |   3   | File operation failure.|
46| ERROR_HTTP_DATA_ERROR<sup>7+</sup> | number |   4   | HTTP transmission failure.|
47| ERROR_INSUFFICIENT_SPACE<sup>7+</sup> | number |   5   | Insufficient storage space.|
48| ERROR_TOO_MANY_REDIRECTS<sup>7+</sup> | number |   6   | Error caused by too many network redirections.|
49| ERROR_UNHANDLED_HTTP_CODE<sup>7+</sup> | number |   7   | Unidentified HTTP code.|
50| ERROR_UNKNOWN<sup>7+</sup> | number |   8   | Unknown error.|
51| ERROR_OFFLINE<sup>9+</sup> | number |   9   | No network connection.|
52| ERROR_UNSUPPORTED_NETWORK_TYPE<sup>9+</sup> | number |   10   | Network type mismatch.|
53
54
55### Causes of Download Pause
56The table below lists the causes of download pause that may be returned by [getTaskInfo<sup>9+</sup>](#gettaskinfo9).
57
58| Name| Type| Value| Description|
59| -------- | -------- | -------- | -------- |
60| 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.|
61| PAUSED_WAITING_FOR_NETWORK<sup>7+</sup> | number |   1   | Download paused due to a network connection problem, for example, network disconnection.|
62| PAUSED_WAITING_TO_RETRY<sup>7+</sup> | number |   2   | Download paused and then retried.|
63| PAUSED_BY_USER<sup>9+</sup> | number |   3   | The user paused the session.|
64| PAUSED_UNKNOWN<sup>7+</sup> | number |   4   | Download paused due to unknown reasons.|
65
66### Download Task Status Codes
67The table below lists the download task status codes that may be returned by [getTaskInfo<sup>9+</sup>](#gettaskinfo9).
68
69| Name| Type| Value| Description|
70| -------- | -------- | -------- | -------- |
71| SESSION_SUCCESSFUL<sup>7+</sup> | number |   0   | Successful download.|
72| SESSION_RUNNING<sup>7+</sup> | number |   1   | Download in progress.|
73| SESSION_PENDING<sup>7+</sup> | number |   2   | Download pending.|
74| SESSION_PAUSED<sup>7+</sup> | number |   3   | Download paused.|
75| SESSION_FAILED<sup>7+</sup> | number |   4   | Download failure without retry.|
76
77
78## request.uploadFile<sup>9+</sup>
79
80uploadFile(context: BaseContext, config: UploadConfig): Promise&lt;UploadTask&gt;
81
82Uploads files. This API uses a promise to return the result.
83
84**Required permissions**: ohos.permission.INTERNET
85
86**System capability**: SystemCapability.MiscServices.Upload
87
88**Parameters**
89
90| Name| Type| Mandatory| Description|
91| -------- | -------- | -------- | -------- |
92| context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.|
93| config | [UploadConfig](#uploadconfig) | Yes| Upload configurations.|
94
95
96**Return value**
97
98| Type| Description|
99| -------- | -------- |
100| Promise&lt;[UploadTask](#uploadtask)&gt; | Promise used to return the **UploadTask** object.|
101
102**Error codes**
103For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md).
104
105| ID| Error Message|
106| -------- | -------- |
107| 13400002 | Bad file path. |
108
109**Example**
110
111  ```js
112  let uploadTask;
113  let uploadConfig = {
114    url: 'https://patch',
115    header: { key1: "value1", key2: "value2" },
116    method: "POST",
117    files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }],
118    data: [{ name: "name123", value: "123" }],
119  };
120  try {
121    request.uploadFile(globalThis.abilityContext, uploadConfig).then((data) => {
122      uploadTask = data;
123    }).catch((err) => {
124        console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
125    });
126  } catch (err) {
127    console.error('err.code : ' + err.code + ', err.message : ' + err.message);
128  }
129  ```
130
131
132## request.uploadFile<sup>9+</sup>
133
134uploadFile(context: BaseContext, config: UploadConfig, callback: AsyncCallback&lt;UploadTask&gt;): void
135
136Uploads files. This API uses an asynchronous callback to return the result.
137
138**Required permissions**: ohos.permission.INTERNET
139
140**System capability**: SystemCapability.MiscServices.Upload
141
142**Parameters**
143
144| Name| Type| Mandatory| Description|
145| -------- | -------- | -------- | -------- |
146| context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.|
147| config | [UploadConfig](#uploadconfig) | Yes| Upload configurations.|
148| callback | AsyncCallback&lt;[UploadTask](#uploadtask)&gt; | Yes| Callback used to return the **UploadTask** object.|
149
150**Error codes**
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  ```js
160  let uploadTask;
161  let uploadConfig = {
162    url: 'https://patch',
163    header: { key1: "value1", key2: "value2" },
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(globalThis.abilityContext, uploadConfig, (err, data) => {
170      if (err) {
171          console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
172          return;
173      }
174      uploadTask = data;
175    });
176  } catch (err) {
177    console.error('err.code : ' + err.code + ', err.message : ' + err.message);
178  }
179  ```
180
181## request.upload<sup>(deprecated)</sup>
182
183upload(config: UploadConfig): Promise&lt;UploadTask&gt;
184
185Uploads files. This API uses a promise to return the result.
186
187**Model restriction**: This API can be used only in the FA model.
188
189>  **NOTE**
190>
191>  This API is deprecated since API version 9. You are advised to use [request.uploadFile<sup>9+</sup>](#requestuploadfile9).
192
193**Required permissions**: ohos.permission.INTERNET
194
195**System capability**: SystemCapability.MiscServices.Upload
196
197**Parameters**
198
199| Name| Type| Mandatory| Description|
200| -------- | -------- | -------- | -------- |
201| config | [UploadConfig](#uploadconfig) | Yes| Upload configurations.|
202
203**Return value**
204
205| Type| Description|
206| -------- | -------- |
207| Promise&lt;[UploadTask](#uploadtask)&gt; | Promise used to return the **UploadTask** object.|
208
209**Example**
210
211  ```js
212  let uploadTask;
213  let uploadConfig = {
214    url: 'https://patch',
215    header: { key1: "value1", key2: "value2" },
216    method: "POST",
217    files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }],
218    data: [{ name: "name123", value: "123" }],
219  };
220  request.upload(uploadConfig).then((data) => {
221      uploadTask = data;
222  }).catch((err) => {
223      console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
224  })
225  ```
226
227
228## request.upload<sup>(deprecated)</sup>
229
230upload(config: UploadConfig, callback: AsyncCallback&lt;UploadTask&gt;): void
231
232Uploads files. This API uses an asynchronous callback to return the result.
233
234**Model restriction**: This API can be used only in the FA model.
235
236>  **NOTE**
237>
238>  This API is deprecated since API version 9. You are advised to use [request.uploadFile<sup>9+</sup>](#requestuploadfile9-1).
239
240**Required permissions**: ohos.permission.INTERNET
241
242**System capability**: SystemCapability.MiscServices.Upload
243
244**Parameters**
245
246| Name| Type| Mandatory| Description|
247| -------- | -------- | -------- | -------- |
248| config | [UploadConfig](#uploadconfig) | Yes| Upload configurations.|
249| callback | AsyncCallback&lt;[UploadTask](#uploadtask)&gt; | Yes| Callback used to return the **UploadTask** object.|
250
251**Example**
252
253  ```js
254  let uploadTask;
255  let uploadConfig = {
256    url: 'https://patch',
257    header: { key1: "value1", key2: "value2" },
258    method: "POST",
259    files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }],
260    data: [{ name: "name123", value: "123" }],
261  };
262  request.upload(uploadConfig, (err, data) => {
263      if (err) {
264          console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
265          return;
266      }
267      uploadTask = data;
268  });
269  ```
270
271## UploadTask
272
273Implements file uploads. Before using any APIs of this class, you must obtain an **UploadTask** object.
274
275### on('progress')
276
277on(type: 'progress', callback:(uploadedSize: number, totalSize: number) =&gt; void): void
278
279Subscribes to an upload event. This API uses an asynchronous callback to return the result.
280
281**Required permissions**: ohos.permission.INTERNET
282
283**System capability**: SystemCapability.MiscServices.Upload
284
285**Parameters**
286
287| Name| Type| Mandatory| Description|
288| -------- | -------- | -------- | -------- |
289| type | string | Yes| Type of the event to subscribe to. The value is **'progress'** (upload progress).|
290| callback | function | Yes| Callback for the upload progress event.|
291
292Parameters of the callback function
293
294| Name| Type| Mandatory| Description|
295| -------- | -------- | -------- | -------- |
296| uploadedSize | number | Yes| Size of the uploaded files, in bits. |
297| totalSize | number | Yes| Total size of the files to upload, in bits. |
298
299**Example**
300
301  ```js
302  uploadTask.on('progress', function callback(uploadedSize, totalSize) {
303      console.info("upload totalSize:" + totalSize + "  uploadedSize:" + uploadedSize);
304  }
305  );
306  ```
307
308
309### on('headerReceive')<sup>7+</sup>
310
311on(type: 'headerReceive', callback:  (header: object) =&gt; void): void
312
313Subscribes to an upload event. This API uses an asynchronous callback to return the result.
314
315**Required permissions**: ohos.permission.INTERNET
316
317**System capability**: SystemCapability.MiscServices.Upload
318
319**Parameters**
320
321| Name| Type| Mandatory| Description|
322| -------- | -------- | -------- | -------- |
323| type | string | Yes| Type of the event to subscribe to. The value is **'headerReceive'** (response header).|
324| callback | function | Yes| Callback for the HTTP Response Header event.|
325
326Parameters of the callback function
327
328| Name| Type| Mandatory| Description|
329| -------- | -------- | -------- | -------- |
330| header | object | Yes| HTTP Response Header.|
331
332**Example**
333
334  ```js
335  uploadTask.on('headerReceive', function callback(headers){
336      console.info("upOnHeader headers:" + JSON.stringify(headers));
337  }
338  );
339  ```
340
341
342### on('complete' | 'fail')<sup>9+</sup>
343
344 on(type:'complete' | 'fail', callback: Callback&lt;Array&lt;TaskState&gt;&gt;): void;
345
346Subscribes to an upload event. This API uses an asynchronous callback to return the result.
347
348**Required permissions**: ohos.permission.INTERNET
349
350**System capability**: SystemCapability.MiscServices.Upload
351
352**Parameters**
353
354| Name| Type| Mandatory| Description|
355| -------- | -------- | -------- | -------- |
356| 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.|
357| callback | Callback&lt;Array&lt;TaskState&gt;&gt; | Yes| Callback used to return the result.|
358
359Parameters of the callback function
360
361| Name| Type| Mandatory| Description|
362| -------- | -------- | -------- | -------- |
363| taskstates | Array&lt;[TaskState](#taskstate9)&gt; | Yes| Upload result.|
364
365**Example**
366
367  ```js
368  uploadTask.on('complete', function callback(taskStates) {
369    for (let i = 0; i < taskStates.length; i++ ) {
370      console.info("upOnComplete taskState:" + JSON.stringify(taskStates[i]));
371    }
372  }
373  );
374
375  uploadTask.on('fail', function callback(taskStates) {
376    for (let i = 0; i < taskStates.length; i++ ) {
377      console.info("upOnFail taskState:" + JSON.stringify(taskStates[i]));
378    }
379  }
380  );
381  ```
382
383
384### off('progress')
385
386off(type:  'progress',  callback?: (uploadedSize: number, totalSize: number) =&gt;  void): void
387
388Unsubscribes from an upload event. This API uses an asynchronous callback to return the result.
389
390**Required permissions**: ohos.permission.INTERNET
391
392**System capability**: SystemCapability.MiscServices.Upload
393
394**Parameters**
395
396| Name| Type| Mandatory| Description|
397| -------- | -------- | -------- | -------- |
398| type | string | Yes| Type of the event to unsubscribe from. The value is **'progress'** (upload progress).|
399| callback | function | No| Callback for the upload progress event.|
400
401Parameters of the callback function
402
403| Name| Type| Mandatory| Description|
404| -------- | -------- | -------- | -------- |
405| uploadedSize | number | Yes| Size of the uploaded files, in bits. |
406| totalSize | number | Yes| Total size of the files to upload, in bits. |
407
408**Example**
409
410  ```js
411  uploadTask.off('progress', function callback(uploadedSize, totalSize) {
412      console.info('uploadedSize: ' + uploadedSize, 'totalSize: ' + totalSize);
413  }
414  );
415  ```
416
417
418### off('headerReceive')<sup>7+</sup>
419
420off(type: 'headerReceive', callback?: (header: object) =&gt; void): void
421
422Unsubscribes from an upload event. This API uses an asynchronous callback to return the result.
423
424**Required permissions**: ohos.permission.INTERNET
425
426**System capability**: SystemCapability.MiscServices.Upload
427
428**Parameters**
429
430| Name| Type| Mandatory| Description|
431| -------- | -------- | -------- | -------- |
432| type | string | Yes| Type of the event to unsubscribe from. The value is **'headerReceive'** (response header).|
433| callback | function | No| Callback for the HTTP Response Header event.|
434
435Parameters of the callback function
436
437| Name| Type| Mandatory| Description|
438| -------- | -------- | -------- | -------- |
439| header | object | Yes| HTTP Response Header.|
440
441**Example**
442
443  ```js
444  uploadTask.off('headerReceive', function callback(headers) {
445      console.info("upOnHeader headers:" + JSON.stringify(headers));
446  }
447  );
448  ```
449
450### off('complete' | 'fail')<sup>9+</sup>
451
452 off(type:'complete' | 'fail', callback?: Callback&lt;Array&lt;TaskState&gt;&gt;): void;
453
454Unsubscribes from an upload event. This API uses an asynchronous callback to return the result.
455
456**Required permissions**: ohos.permission.INTERNET
457
458**System capability**: SystemCapability.MiscServices.Upload
459
460**Parameters**
461
462| Name| Type| Mandatory| Description|
463| -------- | -------- | -------- | -------- |
464| 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.|
465| callback | Callback&lt;Array&lt;TaskState&gt;&gt; | No| Callback used to return the result.|
466
467Parameters of the callback function
468
469| Name| Type| Mandatory| Description|
470| -------- | -------- | -------- | -------- |
471| taskstates | Array&lt;[TaskState](#taskstate9)&gt; | Yes| Upload result.|
472
473**Example**
474
475  ```js
476  uploadTask.off('complete', function callback(taskStates) {
477    for (let i = 0; i < taskStates.length; i++ ) {
478      console.info("upOnComplete taskState:" + JSON.stringify(taskStates[i]));
479    }
480  }
481  );
482
483  uploadTask.off('fail', function callback(taskStates) {
484    for (let i = 0; i < taskStates.length; i++ ) {
485      console.info("upOnFail taskState:" + JSON.stringify(taskStates[i]));
486    }
487  }
488  );
489  ```
490
491### delete<sup>9+</sup>
492delete(): Promise&lt;boolean&gt;
493
494Deletes this upload task. This API uses a promise to return the result.
495
496**Required permissions**: ohos.permission.INTERNET
497
498**System capability**: SystemCapability.MiscServices.Upload
499
500**Return value**
501
502| Type| Description|
503| -------- | -------- |
504| Promise&lt;boolean&gt; | Promise used to return the task removal result. It returns **true** if the operation is successful and returns **false** otherwise.|
505
506**Example**
507
508  ```js
509  uploadTask.delete().then((result) => {
510      if (result) {
511          console.info('Upload task removed successfully. ');
512      } else {
513          console.error('Failed to remove the upload task. ');
514      }
515  }).catch((err) => {
516      console.error('Failed to remove the upload task. Cause: ' + JSON.stringify(err));
517  });
518  ```
519
520
521### delete<sup>9+</sup>
522
523delete(callback: AsyncCallback&lt;boolean&gt;): void
524
525Deletes this upload task. This API uses an asynchronous callback to return the result.
526
527**Required permissions**: ohos.permission.INTERNET
528
529**System capability**: SystemCapability.MiscServices.Upload
530
531**Parameters**
532
533| Name| Type| Mandatory| Description|
534| -------- | -------- | -------- | -------- |
535| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result.|
536
537**Example**
538
539  ```js
540  uploadTask.delete((err, result) => {
541      if (err) {
542          console.error('Failed to remove the upload task. Cause: ' + JSON.stringify(err));
543          return;
544      }
545      if (result) {
546          console.info('Upload task removed successfully.');
547      } else {
548          console.error('Failed to remove the upload task.');
549      }
550  });
551  ```
552
553
554### remove<sup>(deprecated)</sup>
555
556remove(): Promise&lt;boolean&gt;
557
558Removes this upload task. This API uses a promise to return the result.
559
560>  **NOTE**
561>
562>  This API is deprecated since API version 9. You are advised to use [delete<sup>9+</sup>](#delete9).
563
564**Required permissions**: ohos.permission.INTERNET
565
566**System capability**: SystemCapability.MiscServices.Upload
567
568**Return value**
569
570| Type| Description|
571| -------- | -------- |
572| Promise&lt;boolean&gt; | Promise used to return the task removal result. It returns **true** if the operation is successful and returns **false** otherwise.|
573
574**Example**
575
576  ```js
577  uploadTask.remove().then((result) => {
578      if (result) {
579          console.info('Upload task removed successfully. ');
580      } else {
581          console.error('Failed to remove the upload task. ');
582      }
583  }).catch((err) => {
584      console.error('Failed to remove the upload task. Cause: ' + JSON.stringify(err));
585  });
586  ```
587
588
589### remove<sup>(deprecated)</sup>
590
591remove(callback: AsyncCallback&lt;boolean&gt;): void
592
593Removes this upload task. This API uses an asynchronous callback to return the result.
594
595>  **NOTE**
596>
597>  This API is deprecated since API version 9. You are advised to use [delete<sup>9+</sup>](#delete9-1).
598
599**Required permissions**: ohos.permission.INTERNET
600
601**System capability**: SystemCapability.MiscServices.Upload
602
603**Parameters**
604
605| Name| Type| Mandatory| Description|
606| -------- | -------- | -------- | -------- |
607| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result.|
608
609**Example**
610
611  ```js
612  uploadTask.remove((err, result) => {
613      if (err) {
614          console.error('Failed to remove the upload task. Cause: ' + JSON.stringify(err));
615          return;
616      }
617      if (result) {
618          console.info('Upload task removed successfully.');
619      } else {
620          console.error('Failed to remove the upload task.');
621      }
622  });
623  ```
624
625## UploadConfig
626
627**Required permissions**: ohos.permission.INTERNET
628
629**System capability**: SystemCapability.MiscServices.Upload
630
631| Name| Type| Mandatory| Description|
632| -------- | -------- | -------- | -------- |
633| url | string | Yes| Resource URL.|
634| header | Object | Yes| HTTP or HTTPS header added to an upload request.|
635| method | string | Yes| Request method, which can be **'POST'** or **'PUT'**. The default value is **'POST'**.|
636| files | Array&lt;[File](#file)&gt; | Yes| List of files to upload, which is submitted through **multipart/form-data**.|
637| data | Array&lt;[RequestData](#requestdata)&gt; | Yes| Form data in the request body.|
638
639## TaskState<sup>9+</sup>
640
641**Required permissions**: ohos.permission.INTERNET
642
643**System capability**: SystemCapability.MiscServices.Upload
644
645| Name| Type| Mandatory| Description|
646| -------- | -------- | -------- | -------- |
647| path | string | Yes| File path.|
648| responseCode | number | Yes| Return value of an upload task.|
649| message | string | Yes| Description of the upload task result.|
650
651## File
652
653**Required permissions**: ohos.permission.INTERNET
654
655**System capability**: SystemCapability.MiscServices.Download
656
657| Name| Type| Mandatory| Description|
658| -------- | -------- | -------- | -------- |
659| filename | string | Yes| File name in the header when **multipart** is used.|
660| name | string | Yes| Name of a form item when **multipart** is used. The default value is **file**.|
661| uri | string | Yes| Local path for storing files.<br>Only the **internal** protocol type is supported. In the value, **internal://cache/** is mandatory. Example:<br>internal://cache/path/to/file.txt |
662| type | string | Yes| Type of the file content. By default, the type is obtained based on the extension of the file name or URI.|
663
664
665## RequestData
666
667**Required permissions**: ohos.permission.INTERNET
668
669**System capability**: SystemCapability.MiscServices.Download
670
671| Name| Type| Mandatory| Description|
672| -------- | -------- | -------- | -------- |
673| name | string | Yes| Name of a form element.|
674| value | string | Yes| Value of a form element.|
675
676## request.downloadFile<sup>9+</sup>
677
678downloadFile(context: BaseContext, config: DownloadConfig): Promise&lt;DownloadTask&gt;
679
680Downloads files. This API uses a promise to return the result.
681
682**Required permissions**: ohos.permission.INTERNET
683
684**System capability**: SystemCapability.MiscServices.Download
685
686**Parameters**
687
688| Name| Type| Mandatory| Description|
689| -------- | -------- | -------- | -------- |
690| context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.|
691| config | [DownloadConfig](#downloadconfig) | Yes| Download configurations.|
692
693**Return value**
694
695| Type| Description|
696| -------- | -------- |
697| Promise&lt;[DownloadTask](#downloadtask)&gt; | Promise used to return the result.|
698
699**Error codes**
700For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md).
701
702| ID| Error Message|
703| -------- | -------- |
704| 13400001 | File operation error. |
705| 13400002 | Bad file path. |
706| 13400003 | Task manager service error. |
707
708**Example**
709
710  ```js
711  let downloadTask;
712  try {
713    request.downloadFile(globalThis.abilityContext, { url: 'https://xxxx/xxxx.hap' }).then((data) => {
714        downloadTask = data;
715    }).catch((err) => {
716        console.error('Failed to request the download. Cause: ' + JSON.stringify(err));
717    })
718  } catch (err) {
719    console.error('err.code : ' + err.code + ', err.message : ' + err.message);
720  }
721  ```
722
723
724## request.downloadFile<sup>9+</sup>
725
726downloadFile(context: BaseContext, config: DownloadConfig, callback: AsyncCallback&lt;DownloadTask&gt;): void;
727
728Downloads files. This API uses an asynchronous callback to return the result.
729
730**Required permissions**: ohos.permission.INTERNET
731
732**System capability**: SystemCapability.MiscServices.Download
733
734**Parameters**
735
736| Name| Type| Mandatory| Description|
737| -------- | -------- | -------- | -------- |
738| context | [BaseContext](js-apis-inner-application-baseContext.md) | Yes| Application-based context.|
739| config | [DownloadConfig](#downloadconfig) | Yes| Download configurations.|
740| callback | AsyncCallback&lt;[DownloadTask](#downloadtask)&gt; | Yes| Callback used to return the result.|
741
742**Error codes**
743For details about the error codes, see [Upload and Download Error Codes](../errorcodes/errorcode-request.md).
744
745| ID| Error Message|
746| -------- | -------- |
747| 13400001 | File operation error. |
748| 13400002 | Bad file path. |
749| 13400003 | Task manager service error. |
750
751**Example**
752
753  ```js
754  let downloadTask;
755  try {
756    request.downloadFile(globalThis.abilityContext, { url: 'https://xxxx/xxxxx.hap',
757    filePath: 'xxx/xxxxx.hap'}, (err, data) => {
758        if (err) {
759            console.error('Failed to request the download. Cause: ' + JSON.stringify(err));
760            return;
761        }
762        downloadTask = data;
763    });
764  } catch (err) {
765    console.error('err.code : ' + err.code + ', err.message : ' + err.message);
766  }
767  ```
768
769## request.download<sup>(deprecated)</sup>
770
771download(config: DownloadConfig): Promise&lt;DownloadTask&gt;
772
773Downloads files. This API uses a promise to return the result.
774
775>  **NOTE**
776>
777>  This API is deprecated since API version 9. You are advised to use [request.downloadFile<sup>9+</sup>](#requestdownloadfile9).
778
779**Model restriction**: This API can be used only in the FA model.
780
781**Required permissions**: ohos.permission.INTERNET
782
783**System capability**: SystemCapability.MiscServices.Download
784
785**Parameters**
786
787| Name| Type| Mandatory| Description|
788| -------- | -------- | -------- | -------- |
789| config | [DownloadConfig](#downloadconfig) | Yes| Download configurations.|
790
791**Return value**
792
793| Type| Description|
794| -------- | -------- |
795| Promise&lt;[DownloadTask](#downloadtask)&gt; | Promise used to return the result.|
796
797**Example**
798
799  ```js
800  let downloadTask;
801  request.download({ url: 'https://xxxx/xxxx.hap' }).then((data) => {
802      downloadTask = data;
803  }).catch((err) => {
804      console.error('Failed to request the download. Cause: ' + JSON.stringify(err));
805  })
806  ```
807
808
809## request.download<sup>(deprecated)</sup>
810
811download(config: DownloadConfig, callback: AsyncCallback&lt;DownloadTask&gt;): void
812
813Downloads files. This API uses an asynchronous callback to return the result.
814
815>  **NOTE**
816>
817>  This API is deprecated since API version 9. You are advised to use [request.downloadFile<sup>9+</sup>](#requestdownloadfile9-1).
818
819**Model restriction**: This API can be used only in the FA model.
820
821**Required permissions**: ohos.permission.INTERNET
822
823**System capability**: SystemCapability.MiscServices.Download
824
825**Parameters**
826
827| Name| Type| Mandatory| Description|
828| -------- | -------- | -------- | -------- |
829| config | [DownloadConfig](#downloadconfig) | Yes| Download configurations.|
830| callback | AsyncCallback&lt;[DownloadTask](#downloadtask)&gt; | Yes| Callback used to return the result.|
831
832**Example**
833
834  ```js
835  let downloadTask;
836  request.download({ url: 'https://xxxx/xxxxx.hap',
837  filePath: 'xxx/xxxxx.hap'}, (err, data) => {
838      if (err) {
839          console.error('Failed to request the download. Cause: ' + JSON.stringify(err));
840          return;
841      }
842      downloadTask = data;
843  });
844  ```
845
846## DownloadTask
847
848Implements file downloads. Before using any APIs of this class, you must obtain a **DownloadTask** object.
849
850
851### on('progress')
852
853on(type: 'progress', callback:(receivedSize: number, totalSize: number) =&gt; void): void
854
855Subscribes to a download event. This API uses an asynchronous callback to return the result.
856
857**Required permissions**: ohos.permission.INTERNET
858
859**System capability**: SystemCapability.MiscServices.Download
860
861**Parameters**
862
863| Name| Type| Mandatory| Description|
864| -------- | -------- | -------- | -------- |
865| type | string | Yes| Type of the event to subscribe to. The value is **'progress'** (download progress).|
866| callback | function | Yes| Callback for the download progress event.|
867
868Parameters of the callback function
869
870| Name| Type| Mandatory| Description|
871| -------- | -------- | -------- | -------- |
872| receivedSize | number | Yes| Size of the downloaded files, in bits. |
873| totalSize | number | Yes| Total size of the files to download, in bits. |
874
875**Example**
876
877  ```js
878  downloadTask.on('progress', function download_callback(receivedSize, totalSize) {
879      console.info("download receivedSize:" + receivedSize + " totalSize:" + totalSize);
880  }
881  );
882  ```
883
884
885### off('progress')
886
887off(type: 'progress', callback?: (receivedSize: number, totalSize: number) =&gt; void): void
888
889Unsubscribes from a download event. This API uses an asynchronous callback to return the result.
890
891**Required permissions**: ohos.permission.INTERNET
892
893**System capability**: SystemCapability.MiscServices.Download
894
895**Parameters**
896
897| Name| Type| Mandatory| Description|
898| -------- | -------- | -------- | -------- |
899| type | string | Yes| Type of the event to unsubscribe from. The value is **'progress'** (download progress).|
900| callback | function | No| Callback for the download progress event.|
901
902Parameters of the callback function
903
904| Name| Type| Mandatory| Description|
905| -------- | -------- | -------- | -------- |
906| receivedSize | number | Yes| Size of the downloaded files, in bits. |
907| totalSize | number | Yes| Total size of the files to download, in bits. |
908
909**Example**
910
911  ```js
912  downloadTask .off('progress', function download_callback(receivedSize, totalSize) {
913      console.info("download receivedSize:" + receivedSize + " totalSize:" + totalSize);
914  }
915  );
916  ```
917
918
919### on('complete'|'pause'|'remove')<sup>7+</sup>
920
921on(type: 'complete'|'pause'|'remove', callback:() =&gt; void): void
922
923Subscribes to a download event. This API uses an asynchronous callback to return the result.
924
925**Required permissions**: ohos.permission.INTERNET
926
927**System capability**: SystemCapability.MiscServices.Download
928
929**Parameters**
930
931| Name| Type| Mandatory| Description|
932| -------- | -------- | -------- | -------- |
933| 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.|
934| callback | function | Yes| Callback used to return the result.|
935
936**Example**
937
938  ```js
939  downloadTask.on('complete', function callback() {
940      console.info('Download task completed.');
941  }
942  );
943  ```
944
945
946### off('complete'|'pause'|'remove')<sup>7+</sup>
947
948off(type: 'complete'|'pause'|'remove', callback?:() =&gt; void): void
949
950Unsubscribes from a download event. This API uses an asynchronous callback to return the result.
951
952**Required permissions**: ohos.permission.INTERNET
953
954**System capability**: SystemCapability.MiscServices.Download
955
956**Parameters**
957
958| Name| Type| Mandatory| Description|
959| -------- | -------- | -------- | -------- |
960| 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.|
961| callback | function | No| Callback used to return the result.|
962
963**Example**
964
965  ```js
966  downloadTask.off('complete', function callback() {
967      console.info('Download task completed.');
968  }
969  );
970  ```
971
972
973### on('fail')<sup>7+</sup>
974
975on(type: 'fail', callback: (err: number) =&gt; void): void
976
977Subscribes to the download task failure event. This API uses an asynchronous callback to return the result.
978
979**Required permissions**: ohos.permission.INTERNET
980
981**System capability**: SystemCapability.MiscServices.Download
982
983**Parameters**
984
985| Name| Type| Mandatory| Description|
986| -------- | -------- | -------- | -------- |
987| type | string | Yes| Type of the event to subscribe to. The value is **'fail'** (download failure).|
988| callback | function | Yes| Callback for the download task failure event.|
989
990Parameters of the callback function
991
992| Name| Type| Mandatory| Description|
993| -------- | -------- | -------- | -------- |
994| err | number | Yes| Error code of the download failure. For details about the error codes, see [Download Error Codes](#download-error-codes).|
995
996**Example**
997
998  ```js
999  downloadTask.on('fail', function callBack(err) {
1000      console.info('Download task failed. Cause:' + err);
1001  }
1002  );
1003  ```
1004
1005
1006### off('fail')<sup>7+</sup>
1007
1008off(type: 'fail', callback?: (err: number) =&gt; void): void
1009
1010Unsubscribes from the download task failure event. This API uses an asynchronous callback to return the result.
1011
1012**Required permissions**: ohos.permission.INTERNET
1013
1014**System capability**: SystemCapability.MiscServices.Download
1015
1016**Parameters**
1017
1018| Name| Type| Mandatory| Description|
1019| -------- | -------- | -------- | -------- |
1020| type | string | Yes| Type of the event to unsubscribe from. The value is **'fail'** (download failure).|
1021| callback | function | No| Callback for the download task failure event.|
1022
1023Parameters of the callback function
1024
1025| Name| Type| Mandatory| Description|
1026| -------- | -------- | -------- | -------- |
1027| err | number | Yes| Error code of the download failure. For details about the error codes, see [Download Error Codes](#download-error-codes).|
1028
1029**Example**
1030
1031  ```js
1032  downloadTask.off('fail', function callBack(err) {
1033      console.info('Download task failed. Cause:' + err);
1034  }
1035  );
1036  ```
1037
1038### delete<sup>9+</sup>
1039
1040delete(): Promise&lt;boolean&gt;
1041
1042Removes this download task. This API uses a promise to return the result.
1043
1044**Required permissions**: ohos.permission.INTERNET
1045
1046**System capability**: SystemCapability.MiscServices.Download
1047
1048**Return value**
1049
1050| Type| Description|
1051| -------- | -------- |
1052| Promise&lt;boolean&gt; | Promise used to return the task removal result.|
1053
1054**Example**
1055
1056  ```js
1057  downloadTask.delete().then((result) => {
1058      if (result) {
1059          console.info('Download task removed.');
1060      } else {
1061          console.error('Failed to remove the download task.');
1062      }
1063  }).catch ((err) => {
1064      console.error('Failed to remove the download task.');
1065  });
1066  ```
1067
1068
1069### delete<sup>9+</sup>
1070
1071delete(callback: AsyncCallback&lt;boolean&gt;): void
1072
1073Deletes this download task. This API uses an asynchronous callback to return the result.
1074
1075**Required permissions**: ohos.permission.INTERNET
1076
1077**System capability**: SystemCapability.MiscServices.Download
1078
1079**Parameters**
1080
1081| Name| Type| Mandatory| Description|
1082| -------- | -------- | -------- | -------- |
1083| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the task deletion result. |
1084
1085**Example**
1086
1087  ```js
1088  downloadTask.delete((err, result)=>{
1089      if(err) {
1090          console.error('Failed to remove the download task.');
1091          return;
1092      }
1093      if (result) {
1094          console.info('Download task removed.');
1095      } else {
1096          console.error('Failed to remove the download task.');
1097      }
1098  });
1099  ```
1100
1101
1102### getTaskInfo<sup>9+</sup>
1103
1104getTaskInfo(): Promise&lt;DownloadInfo&gt;
1105
1106Obtains the information about this download task. This API uses a promise to return the result.
1107
1108**Required permissions**: ohos.permission.INTERNET
1109
1110**System capability**: SystemCapability.MiscServices.Download
1111
1112**Return value**
1113
1114| Type| Description|
1115| -------- | -------- |
1116| Promise&lt;[DownloadInfo](#downloadinfo7)&gt; | Promise used to return the download task information.|
1117
1118**Example**
1119
1120  ```js
1121  downloadTask.getTaskInfo().then((downloadInfo) => {
1122      console.info('Download task queried. Data:' + JSON.stringify(downloadInfo))
1123  }) .catch((err) => {
1124      console.error('Failed to query the download task. Cause:' + err)
1125  });
1126  ```
1127
1128
1129### getTaskInfo<sup>9+</sup>
1130
1131getTaskInfo(callback: AsyncCallback&lt;DownloadInfo&gt;): void
1132
1133Obtains the information about this download task. This API uses an asynchronous callback to return the result.
1134
1135**Required permissions**: ohos.permission.INTERNET
1136
1137**System capability**: SystemCapability.MiscServices.Download
1138
1139**Parameters**
1140
1141| Name| Type| Mandatory| Description|
1142| -------- | -------- | -------- | -------- |
1143| callback | AsyncCallback&lt;[DownloadInfo](#downloadinfo7)&gt; | Yes| Callback used to return the download task information.|
1144
1145**Example**
1146
1147  ```js
1148  downloadTask.getTaskInfo((err, downloadInfo)=>{
1149      if(err) {
1150          console.error('Failed to query the download mimeType. Cause:' + JSON.stringify(err));
1151      } else {
1152          console.info('download query success. data:'+ JSON.stringify(downloadInfo));
1153      }
1154  });
1155  ```
1156
1157
1158### getTaskMimeType<sup>9+</sup>
1159
1160getTaskMimeType(): Promise&lt;string&gt;
1161
1162Obtains the **MimeType** of this download task. This API uses a promise to return the result.
1163
1164**Required permissions**: ohos.permission.INTERNET
1165
1166**System capability**: SystemCapability.MiscServices.Download
1167
1168**Return value**
1169
1170| Type| Description|
1171| -------- | -------- |
1172| Promise&lt;string&gt; | Promise used to return the **MimeType** of the download task.|
1173
1174**Example**
1175
1176  ```js
1177  downloadTask.getTaskMimeType().then((data) => {
1178      console.info('Download task queried. Data:' + JSON.stringify(data));
1179  }).catch((err) => {
1180      console.error('Failed to query the download MimeType. Cause:' + JSON.stringify(err))
1181  });
1182  ```
1183
1184
1185### getTaskMimeType<sup>9+</sup>
1186
1187getTaskMimeType(callback: AsyncCallback&lt;string&gt;): void;
1188
1189Obtains the **MimeType** of this download task. This API uses an asynchronous callback to return the result.
1190
1191**Required permissions**: ohos.permission.INTERNET
1192
1193**System capability**: SystemCapability.MiscServices.Download
1194
1195**Parameters**
1196
1197| Name| Type| Mandatory| Description|
1198| -------- | -------- | -------- | -------- |
1199| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the **MimeType** of the download task.|
1200
1201**Example**
1202
1203  ```js
1204  downloadTask.getTaskMimeType((err, data)=>{
1205      if(err) {
1206          console.error('Failed to query the download mimeType. Cause:' + JSON.stringify(err));
1207      } else {
1208          console.info('Download task queried. data:' + JSON.stringify(data));
1209      }
1210  });
1211  ```
1212
1213
1214### suspend<sup>9+</sup>
1215
1216suspend(): Promise&lt;boolean&gt;
1217
1218Pauses this download task. This API uses a promise to return the result.
1219
1220**Required permissions**: ohos.permission.INTERNET
1221
1222**System capability**: SystemCapability.MiscServices.Download
1223
1224**Return value**
1225
1226| Type| Description|
1227| -------- | -------- |
1228| Promise&lt;boolean&gt; | Promise used to return the download task pause result.|
1229
1230**Example**
1231
1232  ```js
1233  downloadTask.suspend().then((result) => {
1234      if (result) {
1235           console.info('Download task paused. ');
1236      } else {
1237          console.error('Failed to pause the download task. Cause:' + JSON.stringify(result));
1238      }
1239  }).catch((err) => {
1240      console.error('Failed to pause the download task. Cause:' + JSON.stringify(err));
1241  });
1242  ```
1243
1244
1245### suspend<sup>9+</sup>
1246
1247suspend(callback: AsyncCallback&lt;boolean&gt;): void
1248
1249Pauses this download task. This API uses an asynchronous callback to return the result.
1250
1251**Required permissions**: ohos.permission.INTERNET
1252
1253**System capability**: SystemCapability.MiscServices.Download
1254
1255**Parameters**
1256
1257| Name| Type| Mandatory| Description|
1258| -------- | -------- | -------- | -------- |
1259| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result.|
1260
1261**Example**
1262
1263  ```js
1264  downloadTask.suspend((err, result)=>{
1265      if(err) {
1266          console.error('Failed to pause the download task. Cause:' + JSON.stringify(err));
1267          return;
1268      }
1269      if (result) {
1270           console.info('Download task paused. ');
1271      } else {
1272          console.error('Failed to pause the download task. Cause:' + JSON.stringify(result));
1273      }
1274  });
1275  ```
1276
1277
1278### restore<sup>9+</sup>
1279
1280restore(): Promise&lt;boolean&gt;
1281
1282Resumes this download task. This API uses a promise to return the result.
1283
1284**Required permissions**: ohos.permission.INTERNET
1285
1286**System capability**: SystemCapability.MiscServices.Download
1287
1288**Return value**
1289
1290| Type| Description|
1291| -------- | -------- |
1292| Promise&lt;boolean&gt; | Promise used to return the result.|
1293
1294**Example**
1295
1296  ```js
1297  downloadTask.restore().then((result) => {
1298      if (result) {
1299          console.info('Download task resumed.')
1300      } else {
1301          console.error('Failed to resume the download task. ');
1302      }
1303      console.info('Download task resumed.')
1304  }).catch((err) => {
1305      console.error('Failed to resume the download task. Cause:' + err);
1306  });
1307  ```
1308
1309
1310### restore<sup>9+</sup>
1311
1312restore(callback: AsyncCallback&lt;boolean&gt;): void
1313
1314Resumes this download task. This API uses an asynchronous callback to return the result.
1315
1316**Required permissions**: ohos.permission.INTERNET
1317
1318**System capability**: SystemCapability.MiscServices.Download
1319
1320**Parameters**
1321
1322| Name| Type| Mandatory| Description|
1323| -------- | -------- | -------- | -------- |
1324| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result.|
1325
1326**Example**
1327
1328  ```js
1329  downloadTask.restore((err, result)=>{
1330      if (err) {
1331          console.error('Failed to resume the download task. Cause:' + err);
1332          return;
1333      }
1334      if (result) {
1335          console.info('Download task resumed.');
1336      } else {
1337          console.error('Failed to resume the download task.');
1338      }
1339  });
1340  ```
1341
1342
1343
1344### remove<sup>(deprecated)</sup>
1345
1346remove(): Promise&lt;boolean&gt;
1347
1348Removes this download task. This API uses a promise to return the result.
1349
1350>  **NOTE**
1351>
1352>  This API is deprecated since API version 9. You are advised to use [delete<sup>9+</sup>](#delete9-2).
1353
1354**Required permissions**: ohos.permission.INTERNET
1355
1356**System capability**: SystemCapability.MiscServices.Download
1357
1358**Return value**
1359
1360| Type| Description|
1361| -------- | -------- |
1362| Promise&lt;boolean&gt; | Promise used to return the task removal result.|
1363
1364**Example**
1365
1366  ```js
1367  downloadTask.remove().then((result) => {
1368      if (result) {
1369          console.info('Download task removed.');
1370      } else {
1371          console.error('Failed to remove the download task.');
1372      }
1373  }).catch ((err) => {
1374      console.error('Failed to remove the download task.');
1375  });
1376  ```
1377
1378
1379### remove<sup>(deprecated)</sup>
1380
1381remove(callback: AsyncCallback&lt;boolean&gt;): void
1382
1383Removes this download task. This API uses an asynchronous callback to return the result.
1384
1385>  **NOTE**
1386>
1387>  This API is deprecated since API version 9. You are advised to use [delete<sup>9+</sup>](#delete9-3).
1388
1389**Required permissions**: ohos.permission.INTERNET
1390
1391**System capability**: SystemCapability.MiscServices.Download
1392
1393**Parameters**
1394
1395| Name| Type| Mandatory| Description|
1396| -------- | -------- | -------- | -------- |
1397| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the task removal result.|
1398
1399**Example**
1400
1401  ```js
1402  downloadTask.remove((err, result)=>{
1403      if(err) {
1404          console.error('Failed to remove the download task.');
1405          return;
1406      }
1407      if (result) {
1408          console.info('Download task removed.');
1409      } else {
1410          console.error('Failed to remove the download task.');
1411      }
1412  });
1413  ```
1414
1415
1416### query<sup>(deprecated)</sup>
1417
1418query(): Promise&lt;DownloadInfo&gt;
1419
1420Queries this download task. This API uses a promise to return the result.
1421
1422>  **NOTE**
1423>
1424>  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).
1425
1426**Required permissions**: ohos.permission.INTERNET
1427
1428**System capability**: SystemCapability.MiscServices.Download
1429
1430**Return value**
1431
1432| Type| Description|
1433| -------- | -------- |
1434| Promise&lt;[DownloadInfo](#downloadinfo7)&gt; | Promise used to return the download task information.|
1435
1436**Example**
1437
1438  ```js
1439  downloadTask.query().then((downloadInfo) => {
1440      console.info('Download task queried. Data:' + JSON.stringify(downloadInfo))
1441  }) .catch((err) => {
1442      console.error('Failed to query the download task. Cause:' + err)
1443  });
1444  ```
1445
1446
1447### query<sup>(deprecated)</sup>
1448
1449query(callback: AsyncCallback&lt;DownloadInfo&gt;): void
1450
1451Queries this download task. This API uses an asynchronous callback to return the result.
1452
1453>  **NOTE**
1454>
1455>  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).
1456
1457**Required permissions**: ohos.permission.INTERNET
1458
1459**System capability**: SystemCapability.MiscServices.Download
1460
1461**Parameters**
1462
1463| Name| Type| Mandatory| Description|
1464| -------- | -------- | -------- | -------- |
1465| callback | AsyncCallback&lt;[DownloadInfo](#downloadinfo7)&gt; | Yes| Callback used to return the download task information.|
1466
1467**Example**
1468
1469  ```js
1470  downloadTask.query((err, downloadInfo)=>{
1471      if(err) {
1472          console.error('Failed to query the download mimeType. Cause:' + JSON.stringify(err));
1473      } else {
1474          console.info('download query success. data:'+ JSON.stringify(downloadInfo));
1475      }
1476  });
1477  ```
1478
1479
1480### queryMimeType<sup>(deprecated)</sup>
1481
1482queryMimeType(): Promise&lt;string&gt;
1483
1484Queries the **MimeType** of this download task. This API uses a promise to return the result.
1485
1486>  **NOTE**
1487>
1488>  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).
1489
1490**Required permissions**: ohos.permission.INTERNET
1491
1492**System capability**: SystemCapability.MiscServices.Download
1493
1494**Return value**
1495
1496| Type| Description|
1497| -------- | -------- |
1498| Promise&lt;string&gt; | Promise used to return the **MimeType** of the download task.|
1499
1500**Example**
1501
1502  ```js
1503  downloadTask.queryMimeType().then((data) => {
1504      console.info('Download task queried. Data:' + JSON.stringify(data));
1505  }).catch((err) => {
1506      console.error('Failed to query the download MimeType. Cause:' + JSON.stringify(err))
1507  });
1508  ```
1509
1510
1511### queryMimeType<sup>(deprecated)</sup>
1512
1513queryMimeType(callback: AsyncCallback&lt;string&gt;): void;
1514
1515Queries the **MimeType** of this download task. This API uses an asynchronous callback to return the result.
1516
1517>  **NOTE**
1518>
1519>  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).
1520
1521**Required permissions**: ohos.permission.INTERNET
1522
1523**System capability**: SystemCapability.MiscServices.Download
1524
1525**Parameters**
1526
1527| Name| Type| Mandatory| Description|
1528| -------- | -------- | -------- | -------- |
1529| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the **MimeType** of the download task.|
1530
1531**Example**
1532
1533  ```js
1534  downloadTask.queryMimeType((err, data)=>{
1535      if(err) {
1536          console.error('Failed to query the download mimeType. Cause:' + JSON.stringify(err));
1537      } else {
1538          console.info('Download task queried. data:' + JSON.stringify(data));
1539      }
1540  });
1541  ```
1542
1543
1544### pause<sup>(deprecated)</sup>
1545
1546pause(): Promise&lt;void&gt;
1547
1548Pauses this download task. This API uses a promise to return the result.
1549
1550>  **NOTE**
1551>
1552>  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).
1553
1554**Required permissions**: ohos.permission.INTERNET
1555
1556**System capability**: SystemCapability.MiscServices.Download
1557
1558**Return value**
1559
1560| Type| Description|
1561| -------- | -------- |
1562| Promise&lt;void&gt; | Promise used to return the download task pause result.|
1563
1564**Example**
1565
1566  ```js
1567  downloadTask.pause().then((result) => {
1568      if (result) {
1569           console.info('Download task paused. ');
1570      } else {
1571          console.error('Failed to pause the download task. Cause:' + JSON.stringify(result));
1572      }
1573  }).catch((err) => {
1574      console.error('Failed to pause the download task. Cause:' + JSON.stringify(err));
1575  });
1576  ```
1577
1578
1579### pause<sup>(deprecated)</sup>
1580
1581pause(callback: AsyncCallback&lt;void&gt;): void
1582
1583>  **NOTE**
1584>
1585>  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).
1586
1587Pauses this download task. This API uses an asynchronous callback to return the result.
1588
1589**Required permissions**: ohos.permission.INTERNET
1590
1591**System capability**: SystemCapability.MiscServices.Download
1592
1593**Parameters**
1594
1595| Name| Type| Mandatory| Description|
1596| -------- | -------- | -------- | -------- |
1597| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
1598
1599**Example**
1600
1601  ```js
1602  downloadTask.pause((err, result)=>{
1603      if(err) {
1604          console.error('Failed to pause the download task. Cause:' + JSON.stringify(err));
1605          return;
1606      }
1607      if (result) {
1608           console.info('Download task paused. ');
1609      } else {
1610          console.error('Failed to pause the download task. Cause:' + JSON.stringify(result));
1611      }
1612  });
1613  ```
1614
1615
1616### resume<sup>(deprecated)</sup>
1617
1618resume(): Promise&lt;void&gt;
1619
1620Resumes this download task. This API uses a promise to return the result.
1621
1622>  **NOTE**
1623>
1624>  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).
1625
1626**Required permissions**: ohos.permission.INTERNET
1627
1628**System capability**: SystemCapability.MiscServices.Download
1629
1630**Return value**
1631
1632| Type| Description|
1633| -------- | -------- |
1634| Promise&lt;void&gt; | Promise used to return the result.|
1635
1636**Example**
1637
1638  ```js
1639  downloadTask.resume().then((result) => {
1640      if (result) {
1641          console.info('Download task resumed.')
1642      } else {
1643          console.error('Failed to resume the download task. ');
1644      }
1645      console.info('Download task resumed.')
1646  }).catch((err) => {
1647      console.error('Failed to resume the download task. Cause:' + err);
1648  });
1649  ```
1650
1651
1652### resume<sup>(deprecated)</sup>
1653
1654resume(callback: AsyncCallback&lt;void&gt;): void
1655
1656>  **NOTE**
1657>
1658>  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).
1659
1660Resumes this download task. This API uses an asynchronous callback to return the result.
1661
1662**Required permissions**: ohos.permission.INTERNET
1663
1664**System capability**: SystemCapability.MiscServices.Download
1665
1666**Parameters**
1667
1668| Name| Type| Mandatory| Description|
1669| -------- | -------- | -------- | -------- |
1670| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
1671
1672**Example**
1673
1674  ```js
1675  downloadTask.resume((err, result)=>{
1676      if (err) {
1677          console.error('Failed to resume the download task. Cause:' + err);
1678          return;
1679      }
1680      if (result) {
1681          console.info('Download task resumed.');
1682      } else {
1683          console.error('Failed to resume the download task.');
1684      }
1685  });
1686  ```
1687
1688
1689## DownloadConfig
1690
1691**Required permissions**: ohos.permission.INTERNET
1692
1693**System capability**: SystemCapability.MiscServices.Download
1694
1695| Name| Type| Mandatory| Description|
1696| -------- | -------- | -------- | -------- |
1697| url | string | Yes| Resource URL.|
1698| 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 |
1699| enableMetered | boolean | No| Whether download is allowed on a metered connection.<br>- **true**: allowed<br>- **false**: not allowed |
1700| enableRoaming | boolean | No| Whether download is allowed on a roaming network.<br>- **true**: allowed<br>- **false**: not allowed |
1701| description | string | No| Description of the download session.|
1702| filePath<sup>7+</sup> | string | No| Path where the downloaded file is stored.<br>- filePath:'/data/storage/el2/base/haps/entry/files/test.txt': Save the file to an absolute path.<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 fie path, for example, **'${globalThis.abilityContext.tempDir}/test.txt'**, and store the file in this path.|
1703| networkType | number | No| Network type allowed for download.<br>- NETWORK_MOBILE: 0x00000001<br>- NETWORK_WIFI: 0x00010000|
1704| title | string | No| Download task name.|
1705| background<sup>9+</sup> | boolean | No| Whether to enable the background task notification. When this parameter is enabled, the download status is displayed in the notification panel.|
1706
1707
1708## DownloadInfo<sup>7+</sup>
1709
1710**Required permissions**: ohos.permission.INTERNET
1711
1712**System capability**: SystemCapability.MiscServices.Download
1713
1714| Name| Type| Mandatory| Description|
1715| -------- | -------- | -------- | -------- |
1716| downloadId | number | Yes| ID of the downloaded file.|
1717| failedReason | number | No| Cause of the download failure. The value can be any constant in [Download Error Codes](#download-error-codes).|
1718| fileName | string | Yes| Name of the downloaded file.|
1719| filePath | string | Yes| URI of the saved file.|
1720| pausedReason | number | No| Cause of download pause. The value can be any constant in [Causes of Download Pause](#causes-of-download-pause).|
1721| status | number | Yes| Download task status code. The value can be any constant in [Download Task Status Codes](#download-task-status-codes).|
1722| targetURI | string | Yes| URI of the downloaded file.|
1723| downloadTitle | string | Yes| Download task name.|
1724| downloadTotalBytes | number | Yes| Total size of the files to download, in bytes.|
1725| description | string | Yes| Description of the file to download.|
1726| downloadedBytes | number | Yes| Size of the files downloaded, in bytes.|
1727