跳到主要内容

bn.downloadFile

downloadFile(options): DownloadTask

description Download file from url

example

bn.downloadFile({
url: 'https://example.com/audio/123', //example, not real path
success (res) {
// If only server responses, response data will be write into file and call success callback.
// need to check whether the content is correct if you need.
if (res.statusCode === 200) {
console.log(res.tempFilePath)
}
}
})

Parameters

NameTypeDescription
optionsDownloadFileOptiondownload file options

Returns

DownloadTask

DownloadTask - A task object that can be used to control and query the download

Interface: DownloadFileOption

NameTypeDescription
urlstringrequested resource's url
header?Record<string, string>HTTP request header, cannot set referer here
timeout?numbertimeout**default** 2000
filePath?stringsave path after file is downloaded
success?(result): voidsuccess callback
fail?(res): voidfail callback
complete?(res): voidcomplete callback

success

Optional success(result): void

success callback

Parameters

NameType
resultDownloadSuccessCallbackResult

Returns

void


fail

Optional fail(res): void

fail callback

Parameters

NameType
resGeneralCallbackResult

Returns

void


complete

Optional complete(res): void

complete callback

Parameters

NameType
resGeneralCallbackResult

Returns

void

Interface: DownloadSuccessCallbackResult

NameTypeDescription
filePath?stringFile path. Same as filePath in parameter
statusCodenumberServer HTTP status code
tempFilePath?stringTemp file path, if no filePath in parameter, file will be saved in a temp file path
errMsgstringCall result

Interface: GeneralCallbackResult

NameTypeDescription
errMsgstring

Interface: DownloadTask

NameTypeDescription
abort(): voidabort download task
offHeadersReceived(callback?): voidun-listen HTTP Response Header event
offProgressUpdate(callback?): voidun-listen download task progress update event
onHeadersReceived(callback): voidlisten HTTP Response Header event. Triggered before task completes
onProgressUpdate(callback): voidlisten download task progress update event

abort

abort(): void

abort download task

Returns

void


offHeadersReceived

offHeadersReceived(callback?): void

un-listen HTTP Response Header event

Parameters

NameType
callback?OffHeadersReceivedCallback

Returns

void


offProgressUpdate

offProgressUpdate(callback?): void

un-listen download task progress update event

Parameters

NameType
callback?DownloadTaskOffProgressUpdateCallback

Returns

void


onHeadersReceived

onHeadersReceived(callback): void

listen HTTP Response Header event. Triggered before task completes

Parameters

NameType
callbackOnHeadersReceivedCallback

Returns

void


onProgressUpdate

onProgressUpdate(callback): void

listen download task progress update event

Parameters

NameType
callbackDownloadTaskOnProgressUpdateCallback

Returns

void

OffHeadersReceivedCallback

Ƭ OffHeadersReceivedCallback: (result: OnHeadersReceivedCallbackResult) => void

Type declaration

▸ (result): void

Parameters

NameType
resultOnHeadersReceivedCallbackResult

Returns

void


Interface: OnHeadersReceivedCallbackResult

NameTypeDescription
headerHeaders开发者服务器返回的 HTTP Response Header

DownloadTaskOffProgressUpdateCallback

Ƭ DownloadTaskOffProgressUpdateCallback: (result: DownloadTaskOnProgressUpdateCallbackResult) => void

Type declaration

▸ (result): void

Parameters

NameType
resultDownloadTaskOnProgressUpdateCallbackResult

Returns

void


Interface: DownloadTaskOnProgressUpdateCallbackResult

NameTypeDescription
progressnumber下载进度百分比
totalBytesExpectedToWritenumber预期需要下载的数据总长度,单位 Bytes
totalBytesWrittennumber已经上传的数据长度,单位 Bytes

OnHeadersReceivedCallback

Ƭ OnHeadersReceivedCallback: (result: OnHeadersReceivedCallbackResult) => void

Type declaration

▸ (result): void

Parameters

NameType
resultOnHeadersReceivedCallbackResult

Returns

void


DownloadTaskOnProgressUpdateCallback

Ƭ DownloadTaskOnProgressUpdateCallback: (result: DownloadTaskOnProgressUpdateCallbackResult) => void

Type declaration

▸ (result): void

Parameters

NameType
resultDownloadTaskOnProgressUpdateCallbackResult

Returns

void