Browse SDKs · uni-app / uni-app x
Platform
SDKsuni-app / uni-app x

Upload a file

Upload local files independently with the UniApp SDK and retrieve remote resource URLs.

Copy

The file upload capability functions as an independent asset uploader for user avatars, group icons, profile attachments, or general application assets. It operates independently of the messaging pipeline and does not automatically construct or transmit messages.

Parameters

ParameterTypeRequiredDescription
filepathstringYesLocal file path readable by the native SDK.
namestringYesFilename including file extension.
contentTypestringYesThe MIME type of the file.
uuidstringYesA unique task identifier generated by the client for this upload.
cancelIDstring | nullNoIdentifier used to abort the upload; generate and store this before invoking the upload if cancellation is required.
causestring | nullNoOptional business context or reason for the upload.

Temporary file paths obtained from the photo picker, document picker, or audio recorder can be uploaded directly, but the OS may purge temporary directories at any time. When implementing retry logic or requiring local persistence, move the file into uni.env.USER_DATA_PATH via uni.getFileSystemManager() before persisting the path.

Code example

import { uploadFile } from '@/uni_modules/unix-openim-sdk'

const uploadTaskID = `avatar-${Date.now()}`
const cancelID = `cancel-${uploadTaskID}`

const result = await uploadFile({
  filepath: selectedImagePath,
  name: 'avatar.jpg',
  contentType: 'image/jpeg',
  uuid: uploadTaskID,
  cancelID,
})

if (result?.url != null) {
  await updateProfileAvatar(result.url!)
}

Return value

The Promise resolves with an OpenIMUploadFileResult | null object:

PropertyTypeDescription
urlstring | nullPublic remote URL for the uploaded resource.
uristring | nullInternal storage identifier or path returned by the server.
uuidstring | nullTask identifier matching the upload request.
sizenumber | nullFile size reported by the storage backend.
typnumber | nullStorage resource type integer.
mediaIDstring | nullStorage media identifier.

Only consume non-null properties required by your application. A resolved Promise confirms that the binary upload succeeded; it does not update a user profile or create a message. Trigger subsequent operations explicitly.

Monitor upload progress

The onUploadFileProgress event provides an OpenIMUploadFileProgressEvent | null object containing only progress without a task identifier. Because the event payload does not include a task ID, avoid concurrent uploads if you need to track individual file percentages accurately. For multi-file flows, serialize uploads or limit concurrency.

import {
  OpenIMUploadFileProgressEvent,
  off,
  onUploadFileProgress,
} from '@/uni_modules/unix-openim-sdk'

const handleUploadProgress = (
  event: OpenIMUploadFileProgressEvent | null,
) => {
  if (event == null) return
  setCurrentUploadProgress(event.progress)
}

const uploadProgressSubscription =
  onUploadFileProgress(handleUploadProgress)

// Clean up when destroying view models or switching sessions:
off(uploadProgressSubscription)

To abort an in-flight upload task, pass the saved cancelID to Cancel a file upload.