has.downloadFile(Object object): DownloadTask
Downloads resources to the local host. The maximum size of a file that can be downloaded at a time is 200 MB.
Since version 1.0.4, a DownloadTask object is returned when this API is called.
The API address of the developer server must use the HTTPS protocol and the server domain name must be configured in AppGallery Connect.
You need to specify the Content-Type field in the header of the server response to ensure that the client can correctly process the file type.
For an insecure cipher suite, if the SSL verification fails or the download fails, you are advised to use the has.request API.
The file type is determined as follows:
If filePath is passed, the file type is determined based on filePath.
If filePath is not passed, the file type is determined based on the file name extension extracted from the URL after the last slash (/).
If the file type cannot be determined using either of the preceding methods, the file type is determined based on the Content-Type field in the response header.
Since: 1.0.0
Required permissions: ohos.permission.INTERNET, which must be declared in module.json5.
Precautions: Before calling this API, you need to configure the server domain name.
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| url | string | Yes | URL of the file to be downloaded. |
| header | object | No | Header of an HTTP request. |
| filePath | string | No | Path for saving downloaded files. It can only be a local user file path starting with internal://files/. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Return value of success
| Parameter | Type | Description |
|---|---|---|
| tempFilePath | string | Temporary file path (local path). Downloaded files will be stored in this path if filePath is not specified. |
| filePath | string | User file path, which must be a local path. Returned when filePath is passed, and its value matches the specified filePath. |
| statusCode | number | HTTP status code returned by your server. |
Returns:
A DownloadTask object, which can be used to listen for download progress change events or download cancellation events.
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: 1.url format is invalid. 2.filePath is invalid. 3.permission denied. 4.directory is not exist. |
| 201 | Permission denied. |
| 401 | Parameter error. Possible causes: 1. Missing mandatory parameters. 2. Incorrect parameter type. 3. Parameter verification failed. |
| 40000201 | downloadFile error. For details, see File Management Error Codes. |
Example:
has.downloadFile({
url: 'https://www.example.com/xxx.png', // Replace the example URL with the actual URL.
success: (res) => {
console.info('downloadFile success', res.tempFilePath);
},
fail: (err) => {
console.error('downloadFile fail', err);
},
complete: (res) => {
console.info('downloadFile complete', res);
}
}); DownloadTask.abort()
Stops a download task.
Since: 1.0.4
Error code information
| Error Code | Error Message |
|---|---|
| 201 | Permission denied. |
Example:
const downloadTask = has.downloadFile({
url: 'https://www.example.com/xxx.png', // Replace the example with the actual image resource address.
success: (res) => {
console.info('downloadFile success', res.tempFilePath);
},
fail: (err) => {
console.error('downloadFile fail', err);
},
complete: (res) => {
console.info('downloadFile complete', res);
}
});
downloadTask.abort(); DownloadTask.onProgressUpdate(function callback)
Enables listening for download progress change events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for the download progress change event. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| progress | number | Download progress, in percentage. |
| totalBytesWritten | number | Size of the downloaded data, in bytes. |
| totalBytesExpectedToWrite | number | Anticipated total size of data to be downloaded, in bytes. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
| 401 | Parameter error. Possible causes: 1. Missing mandatory parameters. 2. Incorrect parameter type. 3. Parameter verification failed. |
Example:
const downloadTask = has.downloadFile({
url: 'https://www.example.com/xxx.png', // Replace the example with the actual image resource address.
success: (res) => {
console.info('downloadFile success', res.tempFilePath);
},
fail: (err) => {
console.error('downloadFile fail', err);
},
complete: (res) => {
console.info('downloadFile complete', res);
}
});
const callback = function(res) {
console.info('onProgressUpdate callback triggered', res);
};
downloadTask.onProgressUpdate(callback); DownloadTask.offProgressUpdate(function callback)
Disables listening for download progress change events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by DownloadTask.onProgressUpdate. If this parameter is not passed, all listener functions will be removed. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
| 401 | Parameter error. Possible causes: 1. Missing mandatory parameters. 2. Incorrect parameter type. 3. Parameter verification failed. |
Example:
const downloadTask = has.downloadFile({
url: 'https://www.example.com/xxx.png', // Replace the example with the actual image resource address.
success: (res) => {
console.info('downloadFile success', res.tempFilePath);
},
fail: (err) => {
console.error('downloadFile fail', err);
},
complete: (res) => {
console.info('downloadFile complete', res);
}
});
const callback = function(res) {
console.info('onProgressUpdate callback triggered', res);
}
downloadTask.onProgressUpdate(callback);
downloadTask.offProgressUpdate(callback); DownloadTask.onHeadersReceived(function callback)
Listens for HTTP Response Header events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for the HTTP Response Header event. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| header | object | HTTP response header returned by the server. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
| 401 | Parameter error. Possible causes: 1. Missing mandatory parameters. 2. Incorrect parameter type. 3. Parameter verification failed. |
Example:
const downloadTask = has.downloadFile({
url: 'https://www.example.com/xxx.png', // Replace the example with the actual image resource address.
success: (res) => {
console.info('downloadFile success', res.tempFilePath);
},
fail: (err) => {
console.error('downloadFile fail', err);
},
complete: (res) => {
console.info('downloadFile complete', res);
}
});
const callback = function(res) {
console.info('onHeadersReceived callback triggered', res);
};
downloadTask.onHeadersReceived(callback); DownloadTask.offHeadersReceived(function callback)
Cancels listening for HTTP Response Header events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by DownloadTask.onHeadersReceived. If this parameter is not passed, all listener functions will be removed. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
| 401 | Parameter error. Possible causes: 1. Missing mandatory parameters. 2. Incorrect parameter type. 3. Parameter verification failed. |
Example:
const downloadTask = has.downloadFile({
url: 'https://www.example.com/xxx.png', // Replace the example with the actual image resource address.
success: (res) => {
console.info('downloadFile success', res.tempFilePath);
},
fail: (err) => {
console.error('downloadFile fail', err);
},
complete: (res) => {
console.info('downloadFile complete', res);
}
});
const callback = function(res) {
console.info('onHeadersReceived callback triggered', res);
};
downloadTask.onHeadersReceived(callback);
downloadTask.offHeadersReceived(callback);