• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Upload and Download
2
3
4> **NOTE**<br>
5> 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.
6
7
8## Modules to Import
9
10
11```js
12import request from '@ohos.request';
13```
14
15
16## Constraints
17
18HTTPS is supported by default. To support HTTP, you need to add **network** to the **config.json** file and set the **cleartextTraffic** attribute to **true**.
19
20```
21  "deviceConfig": {
22    "default": {
23      "network": {
24        "cleartextTraffic": true
25      }
26      ...
27    }
28  }
29```
30
31
32## Constants
33
34**Required permissions**: ohos.permission.INTERNET
35
36**System capability**: SystemCapability.MiscServices.Download
37
38| Name | Type | Readable | Writable | Description |
39| -------- | -------- | -------- | -------- | -------- |
40| NETWORK_MOBILE | number | Yes | No | Whether download is allowed when the cellular network is used. |
41| NETWORK_WIFI | number | Yes | No | Whether download is allowed when the WLAN is used. |
42| ERROR_CANNOT_RESUME<sup>7+</sup> | number | Yes | No | Failure to resume the download due to an error. |
43| ERROR_DEVICE_NOT_FOUND<sup>7+</sup> | number | Yes | No | Failure to find a storage device such as an SD card. |
44| ERROR_FILE_ALREADY_EXISTS<sup>7+</sup> | number | Yes | No | Failure to download the file because it already exists. |
45| ERROR_FILE_ERROR<sup>7+</sup> | number | Yes | No | File operation failure. |
46| ERROR_HTTP_DATA_ERROR<sup>7+</sup> | number | Yes | No | HTTP transmission failure. |
47| ERROR_INSUFFICIENT_SPACE<sup>7+</sup> | number | Yes | No | Insufficient storage space. |
48| ERROR_TOO_MANY_REDIRECTS<sup>7+</sup> | number | Yes | No | Error caused by too many network redirections. |
49| ERROR_UNHANDLED_HTTP_CODE<sup>7+</sup> | number | Yes | No | Unidentified HTTP code. |
50| ERROR_UNKNOWN<sup>7+</sup> | number | Yes | No | Unknown error. |
51| PAUSED_QUEUED_FOR_WIFI<sup>7+</sup> | number | Yes | No | Download paused and queuing for WLAN connection, because the file size exceeds the maximum value allowed by a cellular network session. |
52| PAUSED_UNKNOWN<sup>7+</sup> | number | Yes | No | Download paused due to unknown reasons. |
53| PAUSED_WAITING_FOR_NETWORK<sup>7+</sup> | number | Yes | No | Download paused due to a network connection problem, for example, network disconnection. |
54| PAUSED_WAITING_TO_RETRY<sup>7+</sup> | number | Yes | No | Download paused and then retried. |
55| SESSION_FAILED<sup>7+</sup> | number | Yes | No | Download failure without retry. |
56| SESSION_PAUSED<sup>7+</sup> | number | Yes | No | Download paused. |
57| SESSION_PENDING<sup>7+</sup> | number | Yes | No | Download pending. |
58| SESSION_RUNNING<sup>7+</sup> | number | Yes | No | Download in progress. |
59| SESSION_SUCCESSFUL<sup>7+</sup> | number | Yes | No | Successful download. |
60
61
62## request.upload
63
64upload(config: UploadConfig): Promise&lt;UploadTask&gt;
65
66Uploads files. This API uses a promise to return the result.
67
68**Required permissions**: ohos.permission.INTERNET
69
70**System capability**: SystemCapability.MiscServices.Upload
71
72**Parameters**
73
74| Name | Type | Mandatory | Description |
75| -------- | -------- | -------- | -------- |
76| config | [UploadConfig](#uploadconfig) | Yes | Configurations of the upload. |
77
78**Return value**
79
80| Type | Description |
81| -------- | -------- |
82| Promise&lt;[UploadTask](#uploadtask)&gt; | Promise used to return the **UploadTask** object. |
83
84**Example**
85
86  ```js
87  let file1 = { filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" };
88  let data = { name: "name123", value: "123" };
89  let header = { key1: "value1", key2: "value2" };
90  let uploadTask;
91  request.upload({ url: 'https://patch', header: header, method: "POST", files: [file1], data: [data] }).then((data) => {
92      uploadTask = data;
93  }).catch((err) => {
94      console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
95  })
96  ```
97
98
99## request.upload
100
101upload(config: UploadConfig, callback: AsyncCallback&lt;UploadTask&gt;): void
102
103Uploads files. This API uses an asynchronous callback to return the result.
104
105**Required permissions**: ohos.permission.INTERNET
106
107**System capability**: SystemCapability.MiscServices.Upload
108
109**Parameters**
110
111| Name | Type | Mandatory | Description |
112| -------- | -------- | -------- | -------- |
113| config | [UploadConfig](#uploadconfig) | Yes | Configurations of the upload. |
114| callback | AsyncCallback&lt;[UploadTask](#uploadtask)&gt; | No | Callback used to return the **UploadTask** object. |
115
116**Example**
117
118  ```js
119  let file1 = { filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" };
120  let data = { name: "name123", value: "123" };
121  let header = { key1: "value1", key2: "value2" };
122  let uploadTask;
123  request.upload({ url: 'https://patch', header: header, method: "POST", files: [file1], data: [data] }, (err, data) => {
124      if (err) {
125          console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
126          return;
127      }
128      uploadTask = data;
129  });
130  ```
131
132
133## UploadTask
134
135Implements file uploads. Before using any APIs of this class, you must obtain an **UploadTask** object.
136
137
138### on('progress')
139
140on(type: 'progress', callback:(uploadedSize: number, totalSize: number) =&gt; void): void
141
142Subscribes to the upload progress event. This API uses an asynchronous callback to return the result.
143
144**Required permissions**: ohos.permission.INTERNET
145
146**System capability**: SystemCapability.MiscServices.Upload
147
148**Parameters**
149
150| Name | Type | Mandatory | Description |
151| -------- | -------- | -------- | -------- |
152| type | string | Yes | Type of the event to subscribe to. The value is **progress** (upload progress). |
153| callback | function | Yes | Callback for the upload progress event. |
154
155Parameters of the callback function
156
157| Name | Type | Mandatory | Description |
158| -------- | -------- | -------- | -------- |
159| uploadedSize | number | Yes | Size of the uploaded files, in KB. |
160| totalSize | number | Yes | Total size of the files to upload, in KB. |
161
162**Example**
163
164  ```js
165  uploadTask.on('progress', function callback(uploadedSize, totalSize) {
166      console.info("upload totalSize:" + totalSize + "  uploadedSize:" + uploadedSize);
167  }
168  );
169  ```
170
171
172### on('headerReceive')<sup>7+</sup>
173
174on(type: 'headerReceive', callback:  (header: object) =&gt; void): void
175
176Subscribes to the **headerReceive** event, which is triggered when an HTTP response header is received. This API uses an asynchronous callback to return the result.
177
178**Required permissions**: ohos.permission.INTERNET
179
180**System capability**: SystemCapability.MiscServices.Upload
181
182**Parameters**
183
184| Name | Type | Mandatory | Description |
185| -------- | -------- | -------- | -------- |
186| type | string | Yes | Type of the event to subscribe to. The value is **'headerReceive'** (response header). |
187| callback | function | Yes | Callback for the HTTP Response Header event. |
188
189Parameters of the callback function
190
191| Name | Type | Mandatory | Description |
192| -------- | -------- | -------- | -------- |
193| header | object | Yes | HTTP Response Header. |
194
195**Example**
196
197  ```js
198  uploadTask.on('headerReceive', function callback(headers){
199      console.info("upOnHeader headers:" + JSON.stringify(headers));
200  }
201  );
202  ```
203
204
205### off('progress')
206
207off(type:  'progress',  callback?: (uploadedSize: number, totalSize: number) =&gt;  void): void
208
209Unsubscribes from the upload progress event. This API uses an asynchronous callback to return the result.
210
211**Required permissions**: ohos.permission.INTERNET
212
213**System capability**: SystemCapability.MiscServices.Upload
214
215**Parameters**
216
217| Name | Type | Mandatory | Description |
218| -------- | -------- | -------- | -------- |
219| type | string | Yes | Type of the event to unsubscribe from. The value is **'progress'** (upload progress). |
220| callback | function | No | Callback for the upload progress event. |
221
222Parameters of the callback function
223
224| Name | Type | Mandatory | Description |
225| -------- | -------- | -------- | -------- |
226| uploadedSize | number | Yes | Size of the uploaded files, in KB. |
227| totalSize | number | Yes | Total size of the files to upload, in KB. |
228
229**Example**
230
231  ```js
232  uploadTask.off('progress', function callback(uploadedSize, totalSize) {
233      console.info('uploadedSize: ' + uploadedSize, 'totalSize: ' + totalSize);
234  }
235  );
236  ```
237
238
239### off('headerReceive')<sup>7+</sup>
240
241off(type: 'headerReceive', callback?: (header: object) =&gt; void): void
242
243Unsubscribes from the **headerReceive** event. This API uses an asynchronous callback to return the result.
244
245**Required permissions**: ohos.permission.INTERNET
246
247**System capability**: SystemCapability.MiscServices.Upload
248
249**Parameters**
250
251| Name | Type | Mandatory | Description |
252| -------- | -------- | -------- | -------- |
253| type | string | Yes | Type of the event to unsubscribe from. The value is **'headerReceive'** (response header). |
254| callback | function | No | Callback for the HTTP Response Header event. |
255
256Parameters of the callback function
257
258| Name | Type | Mandatory | Description |
259| -------- | -------- | -------- | -------- |
260| header | object | Yes | HTTP Response Header. |
261
262**Example**
263
264  ```js
265  uploadTask.off('headerReceive', function callback(headers) {
266      console.info("upOnHeader headers:" + JSON.stringify(headers));
267  }
268  );
269  ```
270
271
272### remove
273
274remove(): Promise&lt;boolean&gt;
275
276Removes this upload task. This API uses a promise to return the result.
277
278**Required permissions**: ohos.permission.INTERNET
279
280**System capability**: SystemCapability.MiscServices.Upload
281
282**Return value**
283
284| Type | Description |
285| -------- | -------- |
286| Promise&lt;boolean&gt; | Promise used to return the task removal result. If **true** is returned, the task is removed. If **false** is returned, the task fails to be removed. |
287
288**Example**
289
290  ```js
291  uploadTask.remove().then((result) => {
292      if (result) {
293          console.info('Upload task removed successfully. ');
294      } else {
295          console.error('Failed to remove the upload task. ');
296      }
297  }).catch((err) => {
298      console.error('Failed to remove the upload task. Cause: ' + JSON.stringify(err));
299  });
300  ```
301
302
303### remove
304
305remove(callback: AsyncCallback&lt;boolean&gt;): void
306
307Removes this upload task. This API uses an asynchronous callback to return the result.
308
309**Required permissions**: ohos.permission.INTERNET
310
311**System capability**: SystemCapability.MiscServices.Upload
312
313**Parameters**
314
315| Name | Type | Mandatory | Description |
316| -------- | -------- | -------- | -------- |
317| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback used to return the result. |
318
319**Example**
320
321  ```js
322  uploadTask.remove((err, result) => {
323      if (err) {
324          console.error('Failed to remove the upload task. Cause: ' + JSON.stringify(err));
325          return;
326      }
327      if (result) {
328          console.info('Upload task removed successfully.');
329      } else {
330          console.error('Failed to remove the upload task.');
331      }
332  });
333  ```
334
335
336## UploadConfig
337
338**System capability**: SystemCapability.MiscServices.Upload
339
340| Name | Type | Mandatory | Description |
341| -------- | -------- | -------- | -------- |
342| url | string | Yes | Resource URL. |
343| header | object | Yes | HTTP or HTTPS header added to an upload request. |
344| method | string | Yes | Request methods available: **POST** and **PUT**. The default value is **POST**. |
345| files | Array&lt;[File](#file)&gt; | Yes | List of files to upload, which is submitted through **multipart/form-data**. |
346| data | Array&lt;[RequestData](#requestdata)&gt; | Yes | Form data in the request body. |
347
348
349## File
350
351**System capability**: SystemCapability.MiscServices.Upload
352
353| Name | Type | Mandatory | Description |
354| -------- | -------- | -------- | -------- |
355| filename | string | No | File name in the header when **multipart** is used. |
356| name | string | No | Name of a form item when **multipart** is used. The default value is **file**. |
357| uri | string | Yes | Local path for storing files.<br/>The **dataability** and **internal** protocol types are supported. However, the **internal** protocol type supports only temporary directories. Below are examples:<br>dataability:///com.domainname.dataability.persondata/person/10/file.txt<br>internal://cache/path/to/file.txt |
358| type | string | No | Type of the file content. By default, the type is obtained based on the extension of the file name or URI. |
359
360
361## RequestData
362
363**System capability**: SystemCapability.MiscServices.Upload
364
365| Name | Type | Mandatory | Description |
366| -------- | -------- | -------- | -------- |
367| name | string | Yes | Name of a form element. |
368| value | string | Yes | Value of a form element. |
369
370
371## request.download
372
373download(config: DownloadConfig): Promise&lt;DownloadTask&gt;
374
375Downloads files. This API uses a promise to return the result.
376
377**Required permissions**: ohos.permission.INTERNET
378
379**System capability**: SystemCapability.MiscServices.Download
380
381**Parameters**
382
383| Name | Type | Mandatory | Description |
384| -------- | -------- | -------- | -------- |
385| config | [DownloadConfig](#downloadconfig) | Yes | Configurations of the download. |
386
387**Return value**
388
389| Type | Description |
390| -------- | -------- |
391| Promise&lt;[DownloadTask](#downloadtask)&gt; | Promise used to return the result. |
392
393**Example**
394
395  ```js
396  let downloadTask;
397  request.download({ url: 'https://xxxx/xxxx.hap' }).then((data) => {
398      downloadTask = data;
399  }).catch((err) => {
400      console.error('Failed to request the download. Cause: ' + JSON.stringify(err));
401  })
402  ```
403
404
405## request.download
406
407download(config: DownloadConfig, callback: AsyncCallback&lt;DownloadTask&gt;): void
408
409Downloads files. This API uses an asynchronous callback to return the result.
410
411**Required permissions**: ohos.permission.INTERNET
412
413**System capability**: SystemCapability.MiscServices.Download
414
415**Parameters**
416
417| Name | Type | Mandatory | Description |
418| -------- | -------- | -------- | -------- |
419| config | [DownloadConfig](#downloadconfig) | Yes | Configurations of the download. |
420| callback | AsyncCallback&lt;[DownloadTask](#downloadtask)&gt; | No | Callback used to return the result. |
421
422**Example**
423
424  ```js
425  let downloadTask;
426  request.download({ url: 'https://xxxx/xxxxx.hap',
427  filePath: 'xxx/xxxxx.hap'}, (err, data) => {
428      if (err) {
429          console.error('Failed to request the download. Cause: ' + JSON.stringify(err));
430          return;
431      }
432      downloadTask = data;
433  });
434  ```
435
436
437## DownloadTask
438
439Implements file downloads.
440
441
442### on('progress')
443
444on(type: 'progress', callback:(receivedSize: number, totalSize: number) =&gt; void): void
445
446Subscribes to the download progress event. This API uses an asynchronous callback to return the result.
447
448**Required permissions**: ohos.permission.INTERNET
449
450**System capability**: SystemCapability.MiscServices.Download
451
452**Parameters**
453
454| Name | Type | Mandatory | Description |
455| -------- | -------- | -------- | -------- |
456| type | string | Yes | Type of the event to subscribe to. The value is **'progress'** (download progress). |
457| callback | function | Yes | Callback for the download progress event. |
458
459Parameters of the callback function
460
461| Name | Type | Mandatory | Description |
462| -------- | -------- | -------- | -------- |
463| receivedSize | number | Yes | Size of the downloaded files, in KB. |
464| totalSize | number | Yes | Total size of the files to download, in KB. |
465
466**Example**
467
468  ```js
469      downloadTask.on('progress', function download_callback(receivedSize, totalSize) {
470      console.info("download receivedSize:" + receivedSize + " totalSize:" + totalSize);
471  }
472  );
473  });
474  ```
475
476
477### off('progress')
478
479off(type: 'progress', callback?: (receivedSize: number, totalSize: number) =&gt; void): void
480
481Unsubscribes from the download progress event. This API uses an asynchronous callback to return the result.
482
483**Required permissions**: ohos.permission.INTERNET
484
485**System capability**: SystemCapability.MiscServices.Download
486
487**Parameters**
488
489| Name | Type | Mandatory | Description |
490| -------- | -------- | -------- | -------- |
491| type | string | Yes | Type of the event to unsubscribe from. The value is **'progress'** (download progress). |
492| callback | function | No | Callback for the download progress event. |
493
494Parameters of the callback function
495
496| Name | Type | Mandatory | Description |
497| -------- | -------- | -------- | -------- |
498| receivedSize | number | Yes | Size of the downloaded files, in KB. |
499| totalSize | number | Yes | Total size of the files to download, in KB. |
500
501**Example**
502
503  ```js
504      downloadTask .off('progress', function download_callback(receivedSize, totalSize) {
505          console.info("download receivedSize:" + receivedSize + " totalSize:" + totalSize);
506      }
507  );
508  });
509  ```
510
511
512### on('complete'|'pause'|'remove')<sup>7+</sup>
513
514on(type: 'complete'|'pause'|'remove', callback:() =&gt; void): void
515
516Subscribes to a download event. This API uses an asynchronous callback to return the result.
517
518**Required permissions**: ohos.permission.INTERNET
519
520**System capability**: SystemCapability.MiscServices.Download
521
522**Parameters**
523
524| Name | Type | Mandatory | Description |
525| -------- | -------- | -------- | -------- |
526| type | string | Yes | Event type.<br/>- **'complete'**: download task completion event.<br/>- **'pause'**: download task pause event.<br/>- **'remove'**: download task removal event. |
527| callback | function | Yes | Callback used to return the result. |
528
529**Example**
530
531  ```js
532      downloadTask.on('complete', function callback() {
533          console.info('Download task completed.');
534      }
535  );
536  });
537  ```
538
539
540### off('complete'|'pause'|'remove')<sup>7+</sup>
541
542off(type: 'complete'|'pause'|'remove', callback?:() =&gt; void): void
543
544Unsubscribes from the download event. This API uses an asynchronous callback to return the result.
545
546**Required permissions**: ohos.permission.INTERNET
547
548**System capability**: SystemCapability.MiscServices.Download
549
550**Parameters**
551
552| Name | Type | Mandatory | Description |
553| -------- | -------- | -------- | -------- |
554| type | string | Yes | Event type.<br/>- **'complete'**: download task completion event.<br/>- **'pause'**: download task pause event.<br/>- **'remove'**: download task removal event. |
555| callback | function | No | Callback used to return the result. |
556
557**Example**
558
559  ```js
560      downloadTask.off('complete', function callback() {
561          console.info('Download task completed.');
562      }
563  );
564  });
565  ```
566
567
568### on('fail')<sup>7+</sup>
569
570on(type: 'fail', callback: (err: number) =&gt; void): void
571
572Subscribes to the download task failure event. This API uses an asynchronous callback to return the result.
573
574**Required permissions**: ohos.permission.INTERNET
575
576**System capability**: SystemCapability.MiscServices.Download
577
578**Parameters**
579
580| Name | Type | Mandatory | Description |
581| -------- | -------- | -------- | -------- |
582| type | string | Yes | Type of the subscribed event. The value is **'fail'** (download failure). |
583| callback | function | Yes | Callback for the download task failure event. |
584
585Parameters of the callback function
586
587| Name | Type | Mandatory | Description |
588| -------- | -------- | -------- | -------- |
589| err | number | Yes | Error code of the download failure. For details about the error cause, see [ERROR_*](#constants). |
590
591**Example**
592
593  ```js
594      downloadTask.on('fail', function callBack(err) {
595          console.info('Download task failed. Cause:' + err);
596      }
597  );
598  });
599  ```
600
601
602### off('fail')<sup>7+</sup>
603
604off(type: 'fail', callback?: (err: number) =&gt; void): void
605
606Unsubscribes from the download task failure event. This API uses an asynchronous callback to return the result.
607
608**Required permissions**: ohos.permission.INTERNET
609
610**System capability**: SystemCapability.MiscServices.Download
611
612**Parameters**
613
614| Name | Type | Mandatory | Description |
615| -------- | -------- | -------- | -------- |
616| type | string | Yes | Type of the event to unsubscribe from. The value is **'fail'** (download failure). |
617| callback | function | No | Callback for the download task failure event. |
618
619Parameters of the callback function
620
621| Name | Type | Mandatory | Description |
622| -------- | -------- | -------- | -------- |
623| err | number | Yes | Error code of the download failure. For details about the error cause, see [ERROR_*](#constants). |
624
625**Example**
626
627  ```js
628      downloadTask.off('fail', function callBack(err) {
629          console.info('Download task failed. Cause:' + err);
630      }
631  );
632  });
633  ```
634
635
636### remove
637
638remove(): Promise&lt;boolean&gt;
639
640Removes this download task. This API uses a promise to return the result.
641
642**Required permissions**: ohos.permission.INTERNET
643
644**System capability**: SystemCapability.MiscServices.Download
645
646**Return value**
647
648| Type | Description |
649| -------- | -------- |
650| Promise&lt;boolean&gt; | Promise used to return the task removal result. |
651
652**Example**
653
654  ```js
655  downloadTask.remove().then((result) => {
656      if (result) {
657          console.info('Download task removed.');
658      } else {
659          console.error('Failed to remove the download task.');
660      }
661  }).catch ((err) => {
662      console.error('Failed to remove the download task.');
663  });
664  ```
665
666
667### remove
668
669remove(callback: AsyncCallback&lt;boolean&gt;): void
670
671Removes this download task. This API uses an asynchronous callback to return the result.
672
673**Required permissions**: ohos.permission.INTERNET
674
675**System capability**: SystemCapability.MiscServices.Download
676
677**Parameters**
678
679| Name | Type | Mandatory | Description |
680| -------- | -------- | -------- | -------- |
681| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback used to return the task removal result. |
682
683**Example**
684
685  ```js
686  downloadTask.remove((err, result)=>{
687      if(err) {
688          console.error('Failed to remove the download task.');
689          return;
690      }
691      if (result) {
692          console.info('Download task removed.');
693      } else {
694          console.error('Failed to remove the download task.');
695      }
696  });
697  ```
698
699
700### query<sup>7+</sup>
701
702query(): Promise&lt;DownloadInfo&gt;
703
704Queries this download task. This API uses a promise to return the result.
705
706**Required permissions**: ohos.permission.INTERNET
707
708**System capability**: SystemCapability.MiscServices.Download
709
710**Parameters**
711
712| Type | Description |
713| -------- | -------- |
714| Promise&lt;[DownloadInfo](#downloadinfo7)&gt; | Promise used to return the download task information. |
715
716**Example**
717
718  ```js
719  downloadTask.query().then((downloadInfo) => {
720      console.info('Download task queried. Data:' + JSON.stringify(downloadInfo))
721  }) .catch((err) => {
722      console.error('Failed to query the download task. Cause:' + err)
723  });
724  ```
725
726
727### query<sup>7+</sup>
728
729query(callback: AsyncCallback&lt;DownloadInfo&gt;): void
730
731Queries this download task. This API uses an asynchronous callback to return the result.
732
733**Required permissions**: ohos.permission.INTERNET
734
735**System capability**: SystemCapability.MiscServices.Download
736
737**Parameters**
738
739| Name | Type | Mandatory | Description |
740| -------- | -------- | -------- | -------- |
741| callback | AsyncCallback&lt;[DownloadInfo](#downloadinfo7)&gt; | Yes | Callback used to return the download task information. |
742
743**Example**
744
745  ```js
746  downloadTask.query((err, downloadInfo)=>{
747      if(err) {
748          console.error('Failed to query the download mimeType. Cause:' + JSON.stringify(err));
749      } else {
750          console.info('download query success. data:'+ JSON.stringify(downloadInfo));
751      }
752  });
753  ```
754
755
756### queryMimeType<sup>7+</sup>
757
758queryMimeType(): Promise&lt;string&gt;
759
760Queries **MimeType** of this download task. This API uses a promise to return the result.
761
762**Required permissions**: ohos.permission.INTERNET
763
764**System capability**: SystemCapability.MiscServices.Download
765
766**Return value**
767
768| Type | Description |
769| -------- | -------- |
770| Promise&lt;string&gt; | Promise used to return **MimeType** of the download task. |
771
772**Example**
773
774  ```js
775  downloadTask.queryMimeType().then((data) => {
776      console.info('Download task queried. Data:' + JSON.stringify(data));
777  }).catch((err) => {
778      console.error('Failed to query the download MimeType. Cause:' + JSON.stringify(err))
779  });
780  ```
781
782
783### queryMimeType<sup>7+</sup>
784
785queryMimeType(callback: AsyncCallback&lt;string&gt;): void;
786
787Queries **MimeType** of this download task. This API uses an asynchronous callback to return the result.
788
789**Required permissions**: ohos.permission.INTERNET
790
791**System capability**: SystemCapability.MiscServices.Download
792
793**Parameters**
794
795| Name | Type | Mandatory | Description |
796| -------- | -------- | -------- | -------- |
797| callback | AsyncCallback&lt;string&gt; | Yes | Callback used to return **MimeType** of the download task. |
798
799**Example**
800
801  ```js
802  downloadTask.queryMimeType((err, data)=>{
803      if(err) {
804          console.error('Failed to query the download mimeType. Cause:' + JSON.stringify(err));
805      } else {
806          console.info('Download task queried. data:' + JSON.stringify(data));
807      }
808  });
809  ```
810
811
812### pause<sup>7+</sup>
813
814pause(): Promise&lt;void&gt;
815
816Pauses this download task. This API uses a promise to return the result.
817
818**Required permissions**: ohos.permission.INTERNET
819
820**System capability**: SystemCapability.MiscServices.Download
821
822**Return value**
823
824| Type | Description |
825| -------- | -------- |
826| Promise&lt;void&gt; | Promise used to return the download task pause result. |
827
828**Example**
829
830  ```js
831  downloadTask.pause().then((result) => {
832      if (result) {
833           console.info('Download task paused. ');
834      } else {
835          console.error('Failed to pause the download task. Cause:' + JSON.stringify(result));
836      }
837  }).catch((err) => {
838      console.error('Failed to pause the download task. Cause:' + JSON.stringify(err));
839  });
840  ```
841
842
843### pause<sup>7+</sup>
844
845pause(callback: AsyncCallback&lt;void&gt;): void
846
847Pauses this download task. This API uses an asynchronous callback to return the result.
848
849**Required permissions**: ohos.permission.INTERNET
850
851**System capability**: SystemCapability.MiscServices.Download
852
853**Parameters**
854
855| Name | Type | Mandatory | Description |
856| -------- | -------- | -------- | -------- |
857| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result. |
858
859**Example**
860
861  ```js
862  downloadTask.pause((err, result)=>{
863      if(err) {
864          console.error('Failed to pause the download task. Cause:' + JSON.stringify(err));
865          return;
866      }
867      if (result) {
868           console.info('Download task paused. ');
869      } else {
870          console.error('Failed to pause the download task. Cause:' + JSON.stringify(result));
871      }
872  });
873  ```
874
875
876### resume<sup>7+</sup>
877
878resume(): Promise&lt;void&gt;
879
880Resumes this download task. This API uses a promise to return the result.
881
882**Required permissions**: ohos.permission.INTERNET
883
884**System capability**: SystemCapability.MiscServices.Download
885
886**Parameters**
887
888| Type | Description |
889| -------- | -------- |
890| Promise&lt;void&gt; | Promise used to return the result. |
891
892**Example**
893
894  ```js
895  downloadTask.resume().then((result) => {
896      if (result) {
897          console.info('Download task resumed.')
898      } else {
899          console.error('Failed to resume the download task. ');
900      }
901      console.info('Download task resumed.')
902  }).catch((err) => {
903      console.error('Failed to resume the download task. Cause:' + err);
904  });
905  ```
906
907
908### resume<sup>7+</sup>
909
910resume(callback: AsyncCallback&lt;void&gt;): void
911
912Resumes this download task. This API uses an asynchronous callback to return the result.
913
914**Required permissions**: ohos.permission.INTERNET
915
916**System capability**: SystemCapability.MiscServices.Download
917
918**Parameters**
919
920| Name | Type | Mandatory | Description |
921| -------- | -------- | -------- | -------- |
922| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result. |
923
924**Example**
925
926  ```js
927  downloadTask.resume((err, result)=>{
928      if (err) {
929          console.error('Failed to resume the download task. Cause:' + err);
930          return;
931      }
932      if (result) {
933          console.info('Download task resumed.');
934      } else {
935          console.error('Failed to resume the download task.');
936      }
937  });
938  ```
939
940
941## DownloadConfig
942
943**System capability**: SystemCapability.MiscServices.Download
944
945| Name | Type | Mandatory | Description |
946| -------- | -------- | -------- | -------- |
947| url | string | Yes | Resource URL. |
948| header | object | No | HTTP or HTTPS header added to a download request. |
949| enableMetered | boolean | No | Download allowed in metered connections. |
950| enableRoaming | boolean | No | Download allowed on a roaming network. |
951| description | string | No | Description of the download session. |
952| filePath<sup>7+</sup> | string | No | Download path. (The default path is **'internal://cache/'**.<br/>- filePath:'workspace/test.txt': The **workspace** directory is created in the default path to store files.<br/>- filePath:'test.txt': Files are stored in the default path.<br/>- filePath:'workspace/': The **workspace** directory is created in the default path to store files. |
953| networkType | number | No | Network type allowed for download. |
954| title | string | No | Title of the download session. |
955
956
957## DownloadInfo<sup>7+</sup>
958
959**System capability**: SystemCapability.MiscServices.Download
960
961| Name | Type | Mandatory | Description |
962| -------- | -------- | -------- | -------- |
963| downloadId | number | Yes | ID of the downloaded file. |
964| failedReason | number | No | Download failure cause, which can be any constant of [ERROR_*](#constants). |
965| fileName | string | Yes | Name of the downloaded file. |
966| filePath | string | Yes | URI of the saved file. |
967| pausedReason | number | No | Reason for session pause, which can be any constant of [PAUSED_*](#constants). |
968| status | number | Yes | Download status code, which can be any constant of [SESSION_*](#constants). |
969| targetURI | string | Yes | URI of the downloaded file. |
970| downloadTitle | string | Yes | Title of the downloaded file. |
971| downloadTotalBytes | number | Yes | Total size of the downloaded file (int bytes). |
972| description | string | Yes | Description of the file to download. |
973| downloadedBytes | number | Yes | Size of the files downloaded (int bytes). |
974