uni-app uni.chooseMedia小程序真机测试在不同的手机系统上获取的属性不同

uni-app uni.chooseMedia小程序真机测试在不同的手机系统上获取的属性不同

产品分类 PC开发环境操作系统 PC开发环境操作系统版本号 HBuilderX类型 HBuilderX版本号 第三方开发者工具版本号 基础库版本号 项目创建方式
uniapp/小程序/微信 Windows windows7 旗舰版 正式 3.0.7 1.03.2010240 2.14.4 HBuilderX

示例代码:

uni.chooseMedia({ count: 6 - this.uploadLists.length, mediaType: this.mediaType, sizeType: [‘original’, ‘compressed’], //可以指定是原图还是压缩图,默认二者都有 sourceType: this.sourceType, maxDuration: 30, success: (res) => { console.log(‘tempFiles’, res) if (this.action == ‘’) { //未配置上传路径 this.mType = res.type console.log(this.mType); this.$emit(“chooseSuccess”, res.tempFiles); } else { if (this.compress && (res.tempFiles[0].size / 1024 > 1025)) { //设置了需要压缩 并且 文件大于1M,进行压缩上传 this.imgCompress(res.tempFiles); } else { this.imgUpload(res.tempFiles); } }
}});


### 操作步骤:


uni.chooseMedia({
count: 6 - this.uploadLists.length,
mediaType: this.mediaType,
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: this.sourceType,
maxDuration: 30,
success: (res) => {
console.log('tempFiles', res)
if (this.action == '') { //未配置上传路径
this.mType = res.type
console.log(this.mType);
this.$emit("chooseSuccess", res.tempFiles);
} else {
if (this.compress && (res.tempFiles[0].size / 1024 > 1025)) { //设置了需要压缩 并且 文件大于1M,进行压缩上传
this.imgCompress(res.tempFiles);
} else {
this.imgUpload(res.tempFiles);
}
}  
}}); 

预期结果:

uni.chooseMedia({ count: 6 - this.uploadLists.length, mediaType: this.mediaType, sizeType: [‘original’, ‘compressed’], //可以指定是原图还是压缩图,默认二者都有 sourceType: this.sourceType, maxDuration: 30, success: (res) => { console.log(‘tempFiles’, res) if (this.action == ‘’) { //未配置上传路径 this.mType = res.type console.log(this.mType); this.$emit(“chooseSuccess”, res.tempFiles); } else { if (this.compress && (res.tempFiles[0].size / 1024 > 1025)) { //设置了需要压缩 并且 文件大于1M,进行压缩上传 this.imgCompress(res.tempFiles); } else { this.imgUpload(res.tempFiles); } }
}});


### 实际结果:


uni.chooseMedia({
count: 6 - this.uploadLists.length,
mediaType: this.mediaType,
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: this.sourceType,
maxDuration: 30,
success: (res) => {
console.log('tempFiles', res)
if (this.action == '') { //未配置上传路径
this.mType = res.type
console.log(this.mType);
this.$emit("chooseSuccess", res.tempFiles);
} else {
if (this.compress && (res.tempFiles[0].size / 1024 > 1025)) { //设置了需要压缩 并且 文件大于1M,进行压缩上传
this.imgCompress(res.tempFiles);
} else {
this.imgUpload(res.tempFiles);
}
}  
}}); 

bug描述:

uni.chooseMedia 获取到的res.tempFiles 在真机测试中ios没有fileType这个属性


更多关于uni-app uni.chooseMedia小程序真机测试在不同的手机系统上获取的属性不同的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app uni.chooseMedia小程序真机测试在不同的手机系统上获取的属性不同的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个已知的跨平台差异问题。在微信小程序中,uni.chooseMedia API 返回的 tempFiles 对象在不同平台确实存在属性差异。

iOS 平台返回的 tempFiles 对象缺少 fileType 属性,而 Android 平台则包含该属性。这是微信小程序基础库本身的平台差异导致的,不是 uni-app 的 bug。

建议的解决方案:

  1. 不要直接依赖 fileType 属性,可以通过文件后缀名来判断文件类型
  2. 使用 tempFilePath 属性获取文件路径后,自行解析文件类型
  3. 对于 iOS 平台,可以通过其他属性如 size 和 tempFilePath 来推断文件类型

如果需要统一处理,可以这样修改代码:

const file = res.tempFiles[0];
const fileType = file.fileType || file.tempFilePath.split('.').pop().toLowerCase();
回到顶部