Browse SDKs · React Native
Platform
SDKsReact Native

Upload a file

Upload local files independently and obtain remote resource URLs with the OpenIM React Native SDK.

Copy

Upload device local files to the OpenIMServer storage backend (such as MinIO or third-party cloud object storage). Upon successful upload, the SDK returns a publicly accessible resource URL, commonly used to update user avatars, configure group avatars, or pre-upload media assets before creating URL-based messages.

const { url } = await OpenIMSDK.uploadFile({
  name: 'avatar.png',
  contentType: 'image/png',
  uuid: 'avatar-upload-20260827',
  cause: 'user-avatar',
  filepath,
});

Parameters

ParameterTypeRequiredDescription
namestringYesTarget file name for the upload.
contentTypestringYesMIME type of the file (e.g., image/png, video/mp4).
uuidstringYesUnique business identifier for tracking this upload task.
causestringNoBusiness reason tag (such as user-avatar or group-avatar).
filepathstringYesAbsolute path to the local file readable within the application sandbox.

Return value

Resolves to an object containing the remote URL: { url: string }.

Listen for upload status

When uploading large files, monitor upload progress and completion events:

interface UploadProgressPayload {
  current: number;
  size: number;
}

interface UploadCompletePayload {
  data: {
    fileSize: number;
    streamSize: number;
    storageSize: number;
  };
}

const handleUploadProgress = ({ current, size }: UploadProgressPayload) => {
  // Update upload progress percentage in UI
};

const handleUploadComplete = ({ data }: UploadCompletePayload) => {
  // Handle post-upload completion logic
};

// Register listeners
OpenIMSDK.on(OpenIMEvent.UploadOnProgress, handleUploadProgress);
OpenIMSDK.on(OpenIMEvent.UploadComplete, handleUploadComplete);

// Remove listeners
OpenIMSDK.off(OpenIMEvent.UploadOnProgress, handleUploadProgress);
OpenIMSDK.off(OpenIMEvent.UploadComplete, handleUploadComplete);

Remove these listeners when the component unmounts, the user logs out, or accounts switch.