js-apis-system-request.md 6.8 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
# Uploading and Downloading

> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> - The APIs of this module are no longer maintained since API version 6. It is recommended that you use [`@ohos.request`](js-apis-request.md) instead.
> 
> - The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version.


## Modules to Import


```
import request from '@system.request';
```

## Required Permissions

ohos.permission.INTERNET.


## request.upload

upload(Object): void

Uploads files.

**Parameters**

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| url | string | Yes | URL of the upload server. |
| header | Object | No | Request header. |
| method | string | No | Request methods available: **POST** and **PUT**. The default value is **POST**. |
| files | Array<File> | Yes | List of files to upload, which is submitted through **multipart/form-data**. |
| data | Array<RequestData> | No | Form data in the request body. |
| success | Function | No | Called when the download task is complete. |
| fail | Function | No | Called when downloading fails or the task does not exist. |
| complete | Function | No | Called when the execution is complete. |

**Table 1** File

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| filename | string | No | File name in the header when **multipart** is used. |
| name | string | No | Name of a form item when **multipart** is used. The default value is **file**. |
| uri | string | Yes | Local storage path of a file. |
| type | string | No | Type of the file content. By default, the type is obtained based on the suffix of the file name or URI. |

**Table 2** RequestData

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| name | string | Yes | Name of the form element |
| value | string | Yes | Value of the form element |

E
add doc  
ester.zhou 已提交
56
When the files are successfully uploaded, the following values will be returned.
Z
zengyawen 已提交
57 58 59 60 61 62 63

| Name | Type | Description |
| -------- | -------- | -------- |
| code | number | HTTP status code returned by the server. |
| data | string | Content returned by the server. The value type is determined by the type in the returned headers. |
| headers | Object | Headers returned by the server. |

E
add doc  
ester.zhou 已提交
64 65
When the files fail to be uploaded, an HTTP status code is returned in **code** of **data**.

Z
zengyawen 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
**Example**

```
export default {    
  upLoad() {
    request.upload({
      url: 'http://www.path.com',
      files: [
        {
           uri: 'internal://cache/path/to/file.txt',
           name: 'file',
           filename: 'file.txt',
        },
      ],
      data:[
        {
          name: 'name1',
          value: 'value',
         },
       ],
       success: function(data) {
         console.log('upload success, code:' + data.code);
       },
       fail: function() {
         console.log('upload fail');
       },
     });
  }
}
```


## request.download

download(Object): void

Downloads files.

**Parameters**

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| url | string | Yes | Resource URL. |
| header | Object | No | Request header. |
| description | string | No | Download description. The default value is the file name. |
| filename | string | No | Name of the file to download. The value is obtained from the current request or resource URL by default. |
| success | Function | No | Called when the download task is complete. |
| fail | Function | No | Called when downloading fails or the task does not exist. |
| complete | Function | No | Called when the execution is complete. |

Return values of the **success** callback

| Name | Type | Description |
| -------- | -------- | -------- |
| token | string | Download token, which is used to obtain the download status. |

One of the following error codes will be returned if the operation fails.

| Error Code | Description |
| -------- | -------- |
| 400 | Download task failed |

**Example**

```
export default {    
  downLoad() {        
    request.download({            
      url: 'http://www.path.com',            
      success: function(data) {                
        console.log('call success callback success: ' + data.token);            
      },            
      fail: function(data, code) {                
        console.log('handling fail');            
      },        
    });    
  }
}
```


## request.onDownloadComplete

onDownloadComplete(Object): void

Listens to download task status.

**Parameters**

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| token | string | Yes | Token of the result returned by the download method |
| success | Function | No | Called when the download task is complete. |
| fail | Function | No | Called when downloading fails or the task does not exist. |
| complete | Function | No | Called when the execution is complete. |

Return values of the **success** callback

| Name | Type | Description |
| -------- | -------- | -------- |
| uri | string | URI of the download file |

One of the following error codes will be returned if the listening fails.

| Error Code | Description |
| -------- | -------- |
| 400 | Download task failed |
| 401 | Download task not exist |

**Example**

```
export default {    
  onDownloadComplete() {        
    request.onDownloadComplete({            
      token: 'token-index',            
      success: function(data) {                
        console.log('download success, uri:' + data.uri);            
      },            
      fail: function(data, code) {                
        console.log('download fail');            
      },
    });    
  }
}
```