walker9277
walker9277
  • 发布:2024-04-24 15:00
  • 更新:2024-07-29 12:29
  • 阅读:1630

uni.uploadFile app端显示上传失败,"statusCode":500,"errMsg":"uploadFile:ok"

分类:uni-app

场景:
app端调用uni.uploadFile上传非图片、视频格式的文件,选择完文件后得到一个本机绝对路径file:///storage/emulated/0/12.step(或者 /storage/emulated/0/12.step),用于作为filePath参数。

结果:
上传至后端是却返回的是 "statusCode":500,"errMsg":"uploadFile:ok",无法上传
(在H5端上传是正常的)

由于APP端无法支持查看network,都无法查看我是否传了参数给到后端

2024-04-24 15:00 负责人:无 分享
已邀请:
walker9277

walker9277 (作者)

已解决:需要先获取文件读写权限才能使用uni.uploadFile,读取本机文件路径
在APP.vue中加入用户申请同意的文件读写操作
var androidPermissions = {
camera: "android.permission.CAMERA", // 摄像头权限
location: "android.permission.ACCESS_FINE_LOCATION", // 位置权限
calendar: {
read: "android.permission.READ_CALENDAR", // // 日历读取权限
write: "android.permission.WRITE_CALENDAR", // // 日历写入权限
},
storage: {
read: "android.permission.READ_EXTERNAL_STORAGE", // 存储读取权限
write: "android.permission.WRITE_EXTERNAL_STORAGE", // 存储写入权限
},
record: "android.permission.RECORD_AUDIO", // 麦克风权限
contact: {
read: "android.permission.READ_CONTACTS", // 联系人读取权限
write: "android.permission.WRITE_CONTACTS", // 联系人写入权限
},
sms: {
read: "android.permission.READ_SMS", // 短信读取权限
send: "android.permission.SEND_SMS", //短信发送权限
receive: "android.permission.RECEIVE_SMS", // 短信接收权限
},
state: "android.permission.READ_PHONE_STATE", // 手机识别码权限
phone: "android.permission.CALL_PHONE", // 拨打电话权限
log: "android.permission.READ_CALL_LOG", // 通话记录权限
};

export default {  
    onLaunch: function() {  
        console.log('App Launch')  
        let _this = this;  
        // 获取app的权限状态数据  
        let MainActivity = plus.android.runtimeMainActivity();  
        let PermissionCheck = MainActivity.checkSelfPermission('android.permission.READ_EXTERNAL_STORAGE')  
        console.log('MainActivity:', JSON.stringify(PermissionCheck))  
        // PermissionCheck为0:有权限,为-1:没有权限  
        if (PermissionCheck != 0) {  
            uni.showModal({  
                title: '提示',  
                content: '应用保存运行状态等信息,需要获取读写手机存储(系统提示为访问设备上的照片、媒体内容和文件)权限,请允许。',  
                // showCancel: false, // 不显示取消按钮  
                success(res) {  
                    if (res.confirm) {  
                        _this.androidPermissionCheck('storage')  
                    }  
                }  
            });  
        }  
    },  
    onShow: function() {  
        console.log('App Show')  
    },  
    onHide: function() {  
        console.log('App Hide')  
    },  
    methods: {  
        getPermissionList(permissionID) {  
            const id = androidPermissions[permissionID];  
            const list = [];  
            if (typeof id == "string") {  
                list[0] = id;  
            } else {  
                for (let key in id) {  
                    list.push(id[key]);  
                }  
            }  
            return list;  
        },  
        androidPermissionCheck(permissionID) {  
            return new Promise((resolve, reject) => {  
                if (plus) {  
                    let ids = this.getPermissionList(permissionID);  
                    // 调用plus.android.requestPermissions后就默认获取到了手机权限  
                    plus.android.requestPermissions(  
                        ids,  
                        function(result) {  
                            console.log('')  
                            let res = 0;  
                            for (let i = 0; i < result.granted.length; i++) {  
                                let permission = result.granted[i];  
                                console.log("已获取的权限:", permission);  
                                res = 1;  
                            }  
                            for (let i = 0; i < result.deniedPresent.length; i++) {  
                                let permission = result.deniedPresent[i];  
                                console.log("本次已拒绝的权限:", permission);  
                                res = 0;  
                            }  
                            for (let i = 0; i < result.deniedAlways.length; i++) {  
                                let permission = result.deniedAlways[i];  
                                console.log("永久拒绝的权限:", permission);  
                                res = -1;  
                            }  
                            resolve({  
                                code: 1,  
                                data: res,  
                            });  
                        },  
                        (error) => {  
                            reject({  
                                code: 2,  
                                data: `code:${error.code},msg:${error.message}!`,  
                            });  
                        }  
                    );  
                }  
            });  
        }  
    }  
}
7***@qq.com

7***@qq.com

解决了吗,我查看了权限, storage的读写权限已经有了,但是上传还是不行

  • walker9277 (作者)

    解决了,就是权限问题,这个API没有提示,你需要自己判断是否拥有读写权限,如果有,就能正常读取到文件流,你可以重新打包成基座看看

    2024-09-10 14:21

要回复问题请先登录注册