在微信小程序端,上传jpg图片报错。在app端,从相册里选择图片上传报错,直接拍摄正常。上传地址是环信提供的。
//代码如下
openCamera(){
var me = this;
uni.chooseImage({
count: 1,
sizeType: ["original", "compressed"],
sourceType: ["camera"],
success(res){
res.tempFilePaths.forEach((item,index)=>{
me.upLoadImage(item);
})
}
});
},
sendImage(){
var me = this;
uni.chooseImage({
sizeType: ["original", "compressed"],
sourceType: ["album"],
success(res){
console.log(res.tempFilePaths)
res.tempFilePaths.forEach((item,index)=>{
me.upLoadImage(item);
})
},
});
},
upLoadImage(url){
var me = this;
// var tempFilePaths = res.tempFilePaths;
var token = WebIM.conn.context.accessToken
uni.getImageInfo({
src: url,
success(res){
var allowType = {
jpg: true,
gif: true,
png: true,
bmp: true
};
var str =WebIM.config.appkey.split("#");
console.log(str)
var width = res.width;
var height = res.height;
var index = res.path.lastIndexOf(".");
var filetype = (~index && res.path.slice(index + 1)) || "";
if(filetype.toLowerCase() in allowType){
uni.uploadFile({
url: "https://a2.easemob.com/" + str[0] + "/" + str[1] + "/chatfiles",
filePath: url,
name: "file",
header: {
"Content-Type": "multipart/form-data",
Authorization: "Bearer " + token
},
success(res){
var data = res.data;
var dataObj = JSON.parse(data);
console.log(dataObj)
var id = WebIM.conn.getUniqueId(); // 生成本地消息 id
var msg = new WebIM.message(msgType.IMAGE, id);
var file = {
type: msgType.IMAGE,
size: {
width: width,
height: height
},
url: dataObj.uri + "/" + dataObj.entities[0].uuid,
filetype: filetype,
filename: url
};
msg.set({
apiUrl: WebIM.config.apiURL,
body: file,
from: me.from,
to: me.to,
roomType: false,
chatType: msgType.chatType.SINGLE_CHAT,
success: function (argument) {
disp.fire('em.chat.sendSuccess', id);
}
});
// if(me.chatType == msgType.chatType.CHAT_ROOM){
// msg.setGroup("groupchat");
// }
let obj = {
id:msg.id,
self:true,
type:msgType.IMAGE,
value:getMsgData(msg,msgType.IMAGE)
}
WebIM.conn.send(msg.body);
// console.log(getMsgData(msg,msgType.IMAGE))
me.$store.dispatch('pushMsg',obj)
console.log(me.$store.state.msgList)
}
});
}
}
});
},