const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
}
- 发布:2025-10-01 11:07
- 更新:2025-10-09 14:26
- 阅读:74
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: win11
HBuilderX类型: 正式
HBuilderX版本号: 4.75
手机系统: Android
手机系统版本号: Android 15
手机厂商: 小米
手机机型: 小米14
页面类型: vue
vue版本: vue3
打包方式: 云端
项目创建方式: HBuilderX
示例代码:
操作步骤:
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
}
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
} 预期结果:
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
}
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
} 实际结果:
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
}
const Select File = async () => {
try {
const res = await uni.chooseImage({
count: 9, // 允许最多选择9张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
})
// 判断返回值有效性
if (!res || !res.tempFilePaths || res.tempFilePaths.length === 0) {
uni.showToast({
title: '未选择图片',
icon: 'none'
})
return
}
// 显示等待框
uni.showLoading({
title: '图片上传中...'
})
// 支持多图片上传
for (const filePath of res.tempFilePaths) {
await 上传文件(filePath)
}
// 全部上传完成后关闭等待框
uni.hideLoading()
} catch (error) {
uni.hideLoading()
console.error('选择文件失败:', error)
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
} bug描述:
调用相机拍照与选择图片会闪退,相机闪退。