uniapp实现小程序打开相册失败,权限问题
104 浏览2025年04月18日作者:AI_001
	const handleFileUpload = async () => {
		try {
			// 检查相册权限(重点!)
			const { authSetting } = await uni.getSetting()
			if (!authSetting['scope.writePhotosAlbum']) {
				await uni.authorize({ scope: 'scope.writePhotosAlbum' })
			}
			uni.chooseImage({
				count: 1,
				sourceType: ['album'], // 仅从相册选择
				success: (res) => {
					const tempFilePath = res.tempFilePaths[0]
					uni.showToast({ title: '开始上传', icon: 'loading' })

					// 上传操作
					const uploadTask = uni.uploadFile({
						url: 'https://api.example.com/upload', // 替换为真实接口
						filePath: tempFilePath,
						name: 'file',
						success: (uploadRes) => {
							console.log('上传成功响应:', uploadRes.data)
							const data = JSON.parse(uploadRes.data)
							formData.base_image_url = data.url // 更新表单数据

							uni.showToast({ title: '上传成功' })
						},
						fail: (err) => {
							console.error('上传失败:', err)
							uni.showToast({ title: '上传失败', icon: 'none' })
						}
					})

					uploadTask.onProgressUpdate(({ progress }) => {
						console.log('上传进度:', progress + '%')
					})
				}
			})
		} catch (err) {
			uni.showToast({ title: '权限被拒绝', icon: 'none' })
			console.error('权限错误:', err)
		}
	}
  
  提示{errMsg: "authorize:fail platform auth deny", errNo: 10101, errorCode: 103083, errorType: "D"}
最后一次编辑于 2025 年 04 月 18 日
共 9 条评论

相关问答

专题推荐

热门文章

热门问答