uniapp 安卓10+ 选择并上传文件

复制代码
plus.io.chooseFile({
				    title: '选择文件',
				    filetypes: ['mp3'], // 允许的文件类型
				    multiple: false, // 是否允许多选
				}, (res) => {
					console.log('虚拟路径666:', res);
					
					var arr=[
						{
							name: 'files',
							uri: res.files[0],
						}
					]
					let obj = {
						"tableName": "mingmen_daily_miss",
						"demiseUserId": vm.dataId,
						"title": "111.mp3",
						"duration": 2
					}
					
					addAudioFile(arr, {
						form: JSON.stringify(obj)
					}).then(res => {
						vm.$refs.uToast.show({
							title: '上传成功',
							type: 'success',
							// url: ''
						})
						//vm.audioParams.pageNum = 1;
						//vm.audioMethods();
					}).catch(e =>{
						console.log(e);
					})
复制代码
import store from '@/store'
import config from '@/config'
import { getToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import { toast, showConfirm, tansParams } from '@/utils/common'

let timeout = 10000
const baseUrl = config.baseUrl

const upload = config => {
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  config.header = config.header || {}
  if (getToken() && !isToken) {
    config.header['Authorization'] = 'Bearer ' + getToken()
  }
  // get请求映射params参数
  if (config.params) {
    let url = config.url + '?' + tansParams(config.params)
    url = url.slice(0, -1)
    config.url = url
  }
  return new Promise((resolve, reject) => {
      uni.uploadFile({
        timeout: config.timeout || timeout,
        url: baseUrl + config.url,
        filePath: config.filePath,
        name: config.name || 'file',
        header: config.header,
        formData: config.formData,
		files: config.files, // 多文件上传参数 
        success: (res) => {
          let result = JSON.parse(res.data)
          const code = result.code || 200
          const msg = errorCode[code] || result.msg || errorCode['default']
          if (code === 200) {
            resolve(result)
          } else if (code == 401) {
            showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
              if (res.confirm) {
                store.dispatch('LogOut').then(res => {
                  uni.reLaunch({ url: '/pages/login/login' })
                })
              }
            })
            reject('无效的会话,或者会话已过期,请重新登录。')
          } else if (code === 500) {
            toast(msg)
            reject('500')
          } else if (code !== 200) {
            toast(msg)
            reject(code)
          }
        },
        fail: (error) => {
          let { message } = error
          if (message == 'Network Error') {
            message = '后端接口连接异常'
          } else if (message.includes('timeout')) {
            message = '系统接口请求超时'
          } else if (message.includes('Request failed with status code')) {
            message = '系统接口' + message.substr(message.length - 3) + '异常'
          }
          toast(message)
          reject(error)
        }
      })
  })
}

export default upload

把类似文件管理的权限都打开,就应该可以上传文件了

或者使用 APP文件选择(支持安卓和ios) - DCloud 插件市场

相关推荐
不法8 小时前
uniapp 百家云直播插件打包失败
uni-app·插件使用
moxiaoran575314 小时前
uni-app学习笔记五-vue3响应式基础
笔记·学习·uni-app
Mr.app1 天前
uniapp(微信小程序)>关于父子组件的样式传递问题(自定义组件样式穿透)
微信小程序·uni-app
老李不敲代码1 天前
榕壹云搭子系统技术解析:基于Spring Boot+MySQL+UniApp的同城社交平台开发实践
spring boot·mysql·微信小程序·uni-app·软件需求
七七小报1 天前
uniapp-商城-54-后台 新增商品(页面布局)
uni-app
HebyH_1 天前
2025前端面试遇到的问题(vue+uniapp+js+css)
前端·javascript·vue.js·面试·uni-app
gys98951 天前
android studio开发aar插件,并用uniapp开发APP使用这个aar
android·uni-app·android studio
自然 醒2 天前
荣耀手机,系统MagicOS 9.0 USB配置没有音频来源后无法被adb检测到,无法真机调试的解决办法
adb·uni-app
*拯2 天前
Uniapp Android/IOS 获取手机通讯录
android·ios·uni-app
gaojianqiao12342 天前
uniapp引入七鱼客服微信小程序SDK
微信小程序·uni-app