uni-app 使用 uni.chooseImage 发送腾讯IM消息报错
uni-app 使用 uni.chooseImage 发送腾讯IM消息报错
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | windows 10 家庭中文版 19041.867 | HBuilderX |
## 示例代码:
```javascript
uni.chooseImage({
sourceType: [sourceType], // 从相册选择'album',
count: 1, //
sizeType: ['original', 'compressed'],
success: function (res) {
console.log(res)
console.log(res.tempFiles[0])
let message = that.tim.createImageMessage({
to: that.toInfo.code,
conversationType: TIM.TYPES.CONV_C2C,
payload: {file:res.tempFiles[0]},
onProgress: function (event) {}
});
console.log(message)
// 3. 发送图片
let promise = that.tim.sendMessage(message);
promise.then(function (imResponse) {
// 发送成功
}).catch((error)=>{
console.log(error)
})
},
fail: function (e) {
}
})
操作步骤:
下载腾讯相关sdk,并做相关配置,运行代码
预期结果:
发送图片成功
实际结果:
发送失败
bug描述:
当我使用 uni.chooseImage获取图片,发送IM消息会报错,这个没有在接口上保持一致。还有我的是H5页面,不是微信小程序
更多关于uni-app 使用 uni.chooseImage 发送腾讯IM消息报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
请问你是在h5 下面调用的吗 我h5 下面调用TIM 的create 报错 this._socket.onClose is not a function
更多关于uni-app 使用 uni.chooseImage 发送腾讯IM消息报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在H5环境中使用uni.chooseImage获取的图片路径为临时路径(如blob:http://…),而腾讯IM SDK要求的是本地文件路径或网络URL。临时路径无法直接被腾讯IM SDK识别,导致发送失败。
解决方案:
- 将临时文件转换为Base64或File对象后上传到服务器,获取网络URL后再发送。
- 使用uni.uploadFile先将图片上传至自有服务器,获取返回的URL后构造消息:
uni.uploadFile({
url: '你的服务器地址',
filePath: res.tempFilePaths[0],
name: 'file',
success: (uploadRes) => {
const imageUrl = JSON.parse(uploadRes.data).url;
const message = that.tim.createImageMessage({
to: that.toInfo.code,
conversationType: TIM.TYPES.CONV_C2C,
payload: { imageUrl }
});
that.tim.sendMessage(message);
}
});

