9***@qq.com
9***@qq.com
  • 发布:2020-05-21 17:05
  • 更新:2022-06-03 10:27
  • 阅读:1096

环信+uniapp上传图片报错

分类:uni-app

在微信小程序端,上传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)  
                            }  
                        });  
                    }  
                }  
            });  
        },
2020-05-21 17:05 负责人:无 分享
已邀请:
9***@qq.com

9***@qq.com (作者)

这是app端报错

万码奔腾

万码奔腾

你好,请问文章划线标记的功能实现了吗,望指点一下。谢谢

该问题目前已经被锁定, 无法添加新回复