Skip to main content
Version: v3

TransferFile

Start from API_LEVEL 3.0 . Please refer to API_LEVEL.

File Transfer.

Methods

getInbox

Get the receiving file object

getInbox(): Inbox

Inbox

PropertyTypeDescriptionAPI_LEVEL
getNextFile() => FileObjectReturn FileObject to receive the file object3.0
on(eventName: InboxEventName, callback: () => void) => voidListening event, event name reference InboxEventName3.0

FileObject

PropertyTypeDescriptionAPI_LEVEL
sessionIdnumberSession identifier for transferring files3.0
fileNamestringFile name3.0
filePathstringFile path3.0
paramsobjectUser passed parameters3.0
fileSizenumberFile size3.0
readyStateReceiveFileStateFor the status value of the received file, see 'ReceiveFileState'3.0
cancel() => voidCancel a file transfer task3.0
on(eventName: FileEventName, callback: ChangeCallback|ProgressCallback) => voidListen to the file transfer task event, event name reference FileEventName3.0

InboxEventName

ValueTypeDescriptionAPI_LEVEL
NEWFILEstringThe event that just received the file3.0
FILEstringThe event that completed receiving the file3.0

ReceiveFileState

ValueTypeDescriptionAPI_LEVEL
pendingstringPending3.0
transferringstringTransferring3.0
transferredstringTransferred3.0
errorstringError3.0
canceledstringCanceled3.0

FileEventName

ValueTypeDescriptionAPI_LEVEL
changestringThe event name that occurs when readyState changes state, corresponding to the ChangeCallback callback function3.0
progressstringThe event name when the file transfer progress changes, corresponding to the ProgressCallback callback function3.0

ChangeCallback

TypeDescription
(event: ChangeEvent) => voidThe callback that occurs when readyState changes state

ChangeEvent

PropertyTypeDescriptionAPI_LEVEL
type'readyStateChanged'Event type, value is readyStateChanged string3.0
dateChangeEventDataEvent data object, see ChangeEventData for type3.0
timestampnumberUTC timestamp of the event, in milliseconds3.0

ChangeEventData

PropertyTypeDescriptionAPI_LEVEL
readyStatestringFile transfer task status3.0

ProgressCallback

TypeDescription
(event: ProgressEvent) => voidEvent callback function when file transfer progress changes

ProgressEvent

PropertyTypeDescriptionAPI_LEVEL
type'progress'Event type, value is progress string3.0
dateProgressEventDataEvent data object, see ProgressEventData for type3.0
timestampnumberUTC timestamp at the time of the event3.0

ProgressEventData

PropertyTypeDescriptionAPI_LEVEL
fileSizenumberFile size in bytes3.0
loadedSizenumberThe size of the transferred file in bytes3.0

getOutbox

Get the sending file object

getOutbox(): Outbox

Outbox

PropertyTypeDescriptionAPI_LEVEL
enqueueFile(fileName: string, params?: object) => getInbox.FileObjectReturns FileObject, fileName is the path to the file, and params is a customized file transfer object, retrieved from FileObject on the receiving end. The getInbox.FileObject type is referenced above3.0

Example

// Receiving File
import TransferFile from "@zos/ble/TransferFile"

const transferFile = new TransferFile()
const inbox = transferFile.getInbox()

Page({
onInit() {
inbox.on('NEWFILE', function() {
const fileObject = inbox.getNextFile()
fileObject.on('progress', (event) => {
console.log("progress total size", event.data.fileSize)
console.log("progress total size", event.data.loadedSize)
})

fileObject.on('change', (event) => {
if (event.data.readyState === 'transferred') {
console.log('transfered file success')
} else (event.data.readyState === 'error') {
console.log('error')
}
})
})
}
})

// Send File
import TransferFile from "@zos/ble/TransferFile"

const transferFile = new TransferFile()
const outbox = transferFile.getOutbox()

Page({
onInit() {
const fileObject = outbox.enqueueFile("assets://logo.png", { test: 1})

fileObject.on('progress', (event) => {
console.log("progress total size", event.data.fileSize)
console.log("progress total size", event.data.loadedSize)
})

file.on('change', (event) => {
if (event.data.readyState === 'transferred') {
console.log('transfered file success')
} else (event.data.readyState === 'error') {
console.log('error')
}
})
}
})