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 插件市场

相关推荐
寰宇软件2 小时前
PHP房屋出租出售高效预约系统小程序源码
前端·小程序·uni-app·vue·php
尚学教辅学习资料3 小时前
基于SpringBoot的宠物服务系统+uniapp小程序+LW参考示例
spring boot·uni-app·宠物
真的不想写实验3 小时前
uniapp引入ucharts组件不显示
uni-app
烂蜻蜓3 小时前
深入解析 Uniapp 的页面结构
前端·css·vue.js·uni-app·html·html5
Little_Code3 小时前
关于uniapp使用renderJS中调用父类方法和参数的使用
前端·javascript·uni-app
imsopoor3 小时前
【uniapp*vue3】app/h5 webview通讯方案
uni-app
abs6253 小时前
uniapp使用uts插件启动原生安卓Service
android·uni-app·uniapp uts插件·uniapp 安卓服务
貂蝉空大3 小时前
uni-app 开发app 时 ios上传图片失败的问题
uni-app
qq_316837753 小时前
uniapp h5端和app端 使用 turn.js
开发语言·javascript·uni-app
快乐的二进制鸭14 小时前
uniapp实现app的pdf预览
pdf·uni-app