Skip to main content
Version: v3+

Download File

The file download module network.downloader can download network files to a Side Service.

network.downloader module​

downloadFile​

return DownloadTask object

Types​

(options: Options) => DownloadTask

Parameters​

Options: object​

PropertyDescriptionRequiredType
urlFile URLYESstring
timeouttimeoutYESnumber
headersCustomizing the HTTP Request Header fieldNOobject
filePathFile download path, if not specified, defaults to the Side Service's data://download pathNOstring

DownloadTask: object​

cancel​

Cancel the current download task

Types​
() => void
Code Example​
const downloadTask = network.downloader.downloadFile({
url: 'https://docs.zepp.com/zh-cn/img/logo.png',
headers: { key: 121 },
timeout: 60000
})

downloadTask.cancel()

onProgress​

Specify the download progress callback function via the onProgress attribute to return the progress of the download task.

Types​
(event: ProgressEvent) => void

ProgressEvent

PropertyDescriptionType
progressProgress in downloading the file, values 1 - 100number
totalTotal file size in bytesnumber
loadedSize of downloaded file in bytesnumber
Code Example​
const downloadTask = network.downloader.downloadFile({
url: 'https://docs.zepp.com/zh-cn/img/logo.png',
headers: { key: 121 },
timeout: 60000
})

downloadTask.onProgress = (ev) => {
console.log(ev.progress)
console.log(ev.total)
console.log(ev.loaded)
}

onSuccess​

Specify the download success callback function via the onSuccess attribute to return the progress of the download task.

Type​
(event: SuccessEvent) => void

SuccessEvent

PropertyDescriptionType
filePathReturns filePath if filePath is passed in downloadFile, or tempFilePath if no path is specifiedstring
tempFilePathTemporary file path for downloaded filesstring
statusCodeHTTP Status Codenumber
Code Example​
const downloadTask = network.downloader.downloadFile({
url: 'https://docs.zepp.com/zh-cn/img/logo.png',
headers: {},
timeout: 60000,
filePath: 'data://download/1.png'
})

downloadTask.onSuccess = (event) => {
console.log(event.filePath) // data://download/1.png
console.log(event.tempFilePath) // undefined
console.log(event.statusCode) // 200
}

const downloadTask2 = network.downloader.downloadFile({
url: 'https://docs.zepp.com/zh-cn/img/logo.png',
headers: { key: 121 },
timeout: 60000
})

downloadTask2.onSuccess = (event) => {
console.log(event.filePath) // undefined
console.log(event.tempFilePath) // data://download/logo.png
console.log(event.statusCode) // 200
}

onFail​

(event: FailEvent) => void

FailEvent

PropertyDescriptionType
codeError codenumber
messageDetailed error contentsstring
Code Example​
const downloadTask = network.downloader.downloadFile({
url: 'https://docs.zepp.com/zh-cn/img/logo.png',
headers: {},
timeout: 60000,
filePath: 'data://download/1.png'
})

downloadTask.onFail = (event) => {
console.log(event.code)
console.log(event.message)
}

onComplete​

This callback function is called regardless of the success of the download task.

() => void
Code Example​
const downloadTask = network.downloader.downloadFile({
url: 'https://docs.zepp.com/zh-cn/img/logo.png',
headers: {},
timeout: 60000,
filePath: 'data://download/1.png'
})

downloadTask.onComplete = () => {
console.log('do something when success or fail')
}