SDKsWASM
使用文件创建图片消息
使用 WASM SDK 从浏览器 File 创建图片消息。
调用 createImageMessageByFile() 可使用浏览器中的原生图片 File 对象及其元数据构建待发送消息。原图、高清大图和缩略图支持配置不同的尺寸和资源信息;若未针对各层级单独处理,可直接复用相同对象。
参数说明
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
file | File | 是 | 浏览器中的原始图片文件。 |
sourcePicture | Picture | 是 | 原图信息。 |
bigPicture | Picture | 是 | 大图信息。 |
snapshotPicture | Picture | 是 | 缩略图信息。 |
sourcePath | string | 是 | 原始文件的本地名称或业务路径。 |
三个 Picture 参数使用相同的字段结构:
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
Picture.uuid | string | 是 | 图片资源的唯一标识。 |
Picture.type | string | 是 | 图片 MIME 类型。 |
Picture.size | number | 是 | 图片大小,单位为字节。 |
Picture.width | number | 是 | 图片宽度,单位为像素。 |
Picture.height | number | 是 | 图片高度,单位为像素。 |
Picture.url | string | 是 | 图片地址;由文件创建时传空字符串,发送过程中由 SDK 上传。 |
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,
});传入的图片尺寸(宽高)和字节大小必须与实际文件保持一致。接口调用成功后返回待发送的 MessageItem 对象,后续需通过 发送消息 驱动 SDK 执行内置上传并完成消息发送。
这个页面有帮助吗?