跳到主要内容

bn.uploadFile

uploadFile(options): UploadTask

description Uploads local resources to the server. The client initiates a HTTPS POST Request, wherein content-type for multipart/form-data.

example

bn.chooseImage({
success (res) {
const tempFilePaths = res.tempFilePaths
bn.uploadFile({
url: 'https://example/upload', //Example only, not real interface address
filePath: tempFilePaths[0],
name: 'file',
formData: {
'user': 'test'
},
success (res){
const data = res.data
//of the something
}
})
}
})

Parameters

NameType
optionsUploadFileOption

Returns

UploadTask

UploadTask

Interface: UploadFileOption

NameTypeDescription
urlstringDeveloper server address
filePathstringPath to upload file resource (local path)
namestringThe key corresponding to the file, the developer can get the binary content of the file through this key on the server side
header?HeadersHeader of HTTP request, Referer cannot be set in Header
formData?Record<string, string>Other additional form data in the HTTP request
timeout?numberTimeout time.default 2000
method?"POST" | "PUT"remark sdk > 4.9.0; Method used when upload file, support 'POST' or 'PUT'.
complete?UploadFileCompleteCallbackThe callback function for the end of the interface call (it will be executed when the call succeeds or fails)
fail?UploadFileFailCallbackThe callback function of interface call failure
success?UploadFileSuccessCallbackCallback function for successful interface call

Interface: UploadTask

NameTypeDescription
abort(): voidInterrupt upload task
offHeadersReceived(callback?): voidCancel listening to the HTTP Response Header event
offProgressUpdate(callback?): voidCancel listening to upload progress change events
onHeadersReceived(callback): voidListen to the HTTP Response Header event. Will be earlier than the request completion event
onProgressUpdate(callback): voidListen to upload progress change events

abort

abort(): void

Interrupt upload task

Returns

void


offHeadersReceived

offHeadersReceived(callback?): void

Cancel listening to the HTTP Response Header event

Parameters

NameType
callback?OffHeadersReceivedCallback

Returns

void


offProgressUpdate

offProgressUpdate(callback?): void

Cancel listening to upload progress change events

Parameters

NameType
callback?UploadTaskOffProgressUpdateCallback

Returns

void


onHeadersReceived

onHeadersReceived(callback): void

Listen to the HTTP Response Header event. Will be earlier than the request completion event

Parameters

NameType
callbackOnHeadersReceivedCallback

Returns

void


onProgressUpdate

onProgressUpdate(callback): void

Listen to upload progress change events

Parameters

NameType
callbackUploadTaskOnProgressUpdateCallback

Returns

void

OffHeadersReceivedCallback

Ƭ OffHeadersReceivedCallback: (result: OnHeadersReceivedCallbackResult) => void

Type declaration

▸ (result): void

Parameters

NameType
resultOnHeadersReceivedCallbackResult

Returns

void


Interface: OnHeadersReceivedCallbackResult

NameTypeDescription
headerHeadersHTTP Response Header returned by the developer server

UploadTaskOffProgressUpdateCallback

Ƭ UploadTaskOffProgressUpdateCallback: (result: UploadTaskOnProgressUpdateCallbackResult) => void

Type declaration

▸ (result): void

Parameters

NameType
resultUploadTaskOnProgressUpdateCallbackResult

Returns

void


Interface: UploadTaskOnProgressUpdateCallbackResult

NameTypeDescription
progressnumberUpload progress percentage
totalBytesExpectedToSendnumberExpected total length of data to be uploaded, in Bytes
totalBytesSentnumberThe length of the uploaded data, in Bytes

OnHeadersReceivedCallback

Ƭ OnHeadersReceivedCallback: (result: OnHeadersReceivedCallbackResult) => void

Type declaration

▸ (result): void

Parameters

NameType
resultOnHeadersReceivedCallbackResult

Returns

void


UploadTaskOnProgressUpdateCallback

Ƭ UploadTaskOnProgressUpdateCallback: (result: UploadTaskOnProgressUpdateCallbackResult) => void

Type declaration

▸ (result): void

Parameters

NameType
resultUploadTaskOnProgressUpdateCallbackResult

Returns

void