Skip to content

File downloads

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 Download
cancel();

Parameter Description:

ParameterTypeRequiredDescription
urlstringYesFile download link
savePathstringYesSave path (relative to the SDK file storage directory)
headersobjectNoRequest Header
onStartfunctionNoDownload callback
onProgressfunctionNoProgress callback, triggered approximately every 300ms.
onSuccessfunctionNosuccess callback
onFailfunctionNoFailure callback

onProgress callback parameters:

ParameterTypeDescription
progressnumberDownload progress 0-100
downloadedBytesnumberBytes Downloaded
totalBytesnumberTotal file size in bytes

onSuccess callback parameters:

ParameterTypeDescription
filePathstringAbsolute file path
downloadedBytesnumberBytes Downloaded
totalBytesnumberTotal file size in bytes

onFail callback parameters:

ParameterTypeDescription
errorstringError 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:

ParameterTypeRequiredDescription
fileUrlstringYesFile download link
fileMd5stringNoFile MD5; skip MD5 verification if empty.
cacheTtsnumberNoCache validity period, in seconds; when <= 0, force re-download.
fileCacheKeystringNoCustom 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 the fileUrl without the query string.
  • Native converts the cache key into a secure filename and does not directly concatenate the URL to the file path.
  • When fileMd5 is 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:

ParameterTypeDescription
filePathstringAbsolute path of the local cache file
cacheHitbooleanWhether cache was hit
cacheKeystringThe actual original cache key used this time