File downloads
downloadFile(options)
Section titled “downloadFile(options)”Download files to local storage.
const cancel = SuperSDK.downloadFile({ url: 'https://example.com/file.zip', savePath: 'downloads/file.zip', headers: { 'Authorization': 'Bearer token123' }, onStart: () => { console.log('Download started'); }, onProgress: ({ progress, downloadedBytes, totalBytes }) => { console.log(`Download progress: ${progress}%`); }, onSuccess: ({ filePath, downloadedBytes, totalBytes }) => { console.log('Download complete:', filePath); }, onFail: ({ error }) => { console.error('Download failed:', error); }});
// Cancel Downloadcancel();Parameter Description:
| Parameter | Type | Required | Description |
|---|---|---|---|
| url | string | Yes | File download link |
| savePath | string | Yes | Save path (relative to the SDK file storage directory) |
| headers | object | No | Request Header |
| onStart | function | No | Download callback |
| onProgress | function | No | Progress callback, triggered approximately every 300ms. |
| onSuccess | function | No | success callback |
| onFail | function | No | Failure callback |
onProgress callback parameters:
| Parameter | Type | Description |
|---|---|---|
| progress | number | Download progress 0-100 |
| downloadedBytes | number | Bytes Downloaded |
| totalBytes | number | Total file size in bytes |
onSuccess callback parameters:
| Parameter | Type | Description |
|---|---|---|
| filePath | string | Absolute file path |
| downloadedBytes | number | Bytes Downloaded |
| totalBytes | number | Total file size in bytes |
onFail callback parameters:
| Parameter | Type | Description |
|---|---|---|
| error | string | Error Message |
downloadWithCache(fileUrl, fileMd5, cacheTts, fileCacheKey)
Section titled “downloadWithCache(fileUrl, fileMd5, cacheTts, fileCacheKey)”Download the file and write it to the SDK cache directory. If the cache is hit, do not re-download; directly return the local file path.
const result = await SuperSDK.downloadWithCache( 'https://example.com/assets/file.zip?token=abc', 'd41d8cd98f00b204e9800998ecf8427e', 86400, 'asset-file-v1');
console.log(result);// { filePath: '/path/to/download_cache/xxx.zip', cacheHit: true, cacheKey: 'asset-file-v1' }Parameter Description:
| Parameter | Type | Required | Description |
|---|---|---|---|
| fileUrl | string | Yes | File download link |
| fileMd5 | string | No | File MD5; skip MD5 verification if empty. |
| cacheTts | number | No | Cache validity period, in seconds; when <= 0, force re-download. |
| fileCacheKey | string | No | Custom cache key; if empty or not provided, use fileUrl without the query string. |
Caching Rules:
- The cache key prioritizes the non-empty
fileCacheKey; otherwise, it uses thefileUrlwithout the query string. - Native converts the cache key into a secure filename and does not directly concatenate the URL to the file path.
- When
fileMd5is not empty, the cached file and the newly downloaded file are verified. If the MD5 of the newly downloaded file does not match, the operation fails and the old cache is not overwritten.
Return Value:
| Parameter | Type | Description |
|---|---|---|
| filePath | string | Absolute path of the local cache file |
| cacheHit | boolean | Whether cache was hit |
| cacheKey | string | The actual original cache key used this time |