uni-app 安卓无法上传视频
uni-app 安卓无法上传视频
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | win10 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
HBuilderX类型:正式
HBuilderX版本号:3.1.10
手机系统:Android
手机系统版本号:Android 11
手机厂商:华为
手机机型:所有安卓
页面类型:vue
打包方式:云端
示例代码:
uni.chooseVideo({
compressed: true,
sourceType: ['album', 'camera'], // 在相册中选择,还是利用照相机进行拍摄
maxDuration: 30,
success: function(res) { // 回调函数:再选择完视频之后,会将选择的视频文件传给res
uni.uploadFile({
url: "https://weapp.esionet.com/api/common/uploadVideo", //后台给的阿里云存储给的上传地址
name: 'file',
filePath: res.tempFilePath,
formData: {
token: '0b1e8daf-e3a6-4a31-8070-bc61815d0bf6',
},
success: res => {
console.info(res)
}
});
}
})
操作步骤: 1.选择视频 2.上传到后端服务器
预期结果: 提交文件,上传成功
实际结果: 通过抓包发现,安卓机子 没提交选择的文件
bug描述: app安卓无法上传视频!通过抓包发现,请求中 没有附带提交选择的文件。ios/小程序正常
更多关于uni-app 安卓无法上传视频的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
这是一个典型的uni-app在Android平台上文件上传路径处理的问题。从抓包结果来看,Android设备没有附带提交文件,说明res.tempFilePath在Android平台可能无法被正确识别。
问题分析:
- Android平台对临时文件路径的处理与iOS不同
tempFilePath在Android上可能不是有效的文件路径- 云端打包可能对文件系统访问权限有限制
解决方案:
uni.chooseVideo({
compressed: true,
sourceType: ['album', 'camera'],
maxDuration: 30,
success: function(res) {
// 在Android平台需要特殊处理文件路径
let filePath = res.tempFilePath;
// 检查文件是否存在
plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
uni.uploadFile({
url: "https://weapp.esionet.com/api/common/uploadVideo",
name: 'file',
filePath: filePath,
formData: {
token: '0b1e8daf-e3a6-4a31-8070-bc61815d0bf6',
},
success: res => {
console.info('上传成功:', res)
},
fail: err => {
console.error('上传失败:', err)
}
});
}, function(e) {
console.error('文件不存在:', filePath)
});
},
fail: function(err) {
console.error('选择视频失败:', err)
}
})

