Browse SDKs · WASM
SDKsWASM

Create an image message from a file

Create an image message from a browser File with the WASM SDK.

Copy

createImageMessageByFile() creates a message from a browser file and its image metadata. The source image, large image, and thumbnail can have different dimensions and resource information. The example reuses one object only because all three variants are identical in this case.

Parameters

ParameterTypeRequiredDescription
fileFileYesOriginal image file from the browser.
sourcePicturePicBaseInfoYesSource image information.
bigPicturePicBaseInfoYesLarge image information.
snapshotPicturePicBaseInfoYesThumbnail information.
sourcePathstringYesLocal filename or application-defined path of the source file.

All three PicBaseInfo parameters use the same field structure:

ParameterTypeRequiredDescription
PicBaseInfo.uuidstringYesUnique identifier for the image resource.
PicBaseInfo.typestringYesImage MIME type.
PicBaseInfo.sizenumberYesImage size in bytes.
PicBaseInfo.widthnumberYesImage width in pixels.
PicBaseInfo.heightnumberYesImage height in pixels.
PicBaseInfo.urlstringYesImage URL. Pass an empty string when creating from a file; the SDK uploads the image during sending.
const picture = {
  uuid: crypto.randomUUID(),
  type: file.type,
  size: file.size,
  width,
  height,
  url: '',
};
const { data: message } = await openimsdk.createImageMessageByFile({
  file,
  sourcePicture: picture,
  bigPicture: picture,
  snapshotPicture: picture,
  sourcePath: file.name,
});

Derive the image dimensions and size from the actual file. When the Promise resolves, it has created only a pending message object. Use sendMessage() afterward to upload and send it.