Back to Uni App

Choose File

docs/api/choose-file.md

2.3.316.0 KB
Original Source
<!-- ## uni.chooseFile(options) @choosefile -->

::: sourceCode

uni.chooseFile(options) @choosefile

GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-media

GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-media

:::

从本地选择文件

chooseFile 兼容性

Web微信小程序AndroidiOSHarmonyOSHarmonyOS(Vapor)
4.04.414.514.614.615.0

参数

名称类型必填默认值兼容性描述
optionsChooseFileOptions-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -

options 的属性描述

名称类型必备默认值兼容性描述
countnumber-Web: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x最多可以选择的文件数,默认100,注意Android中count只会决定是否是单选/多选,如果count>1 是多选效果,等于1为单选效果,
typestring-Web: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x选择文件的类型,默认all,可选
  • image: 选择图片文件
  • video: 选择视频文件
  • audio: 选择音频文件
  • all: 默认值,选择本地文件,包含图片和视频 | | extension | Array<string> | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x | 选择文件的后缀名,暂只支持.zip、.png等,不支持application/msword等值, App平台不支持 | | sizeType | any | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x | original 原图,compressed 压缩图,默认二者都有, App平台不支持 | | sourceType | Array<string> | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x | album 从相册选图,camera 使用相机,默认二者都有, App平台不支持 | | success | (result: ChooseFileSuccess) => void | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x | 成功则返回图片的本地文件路径列表 tempFilePaths、tempFiles | | fail | (result: ChooseFileFail) => void | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x | 接口调用失败的回调函数 | | complete | (result: any) => void | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x | 接口调用结束的回调函数(调用成功、失败都会执行) |
type 的属性描述
合法值兼容性描述
imageWeb: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x图片类型
videoWeb: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x视频类型
audioWeb: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x音频类型
allWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --

ChooseFileSuccess 的属性值 @choosefilesuccess-values

名称类型必备默认值兼容性描述
tempFilePathsArray<string>-Web: 4.0; 微信小程序: 4.41; Android: x; iOS: 4.61; HarmonyOS: x文件的本地文件路径列表, Android平台不支持
tempFilesArray<ChooseFileTempFile>-Web: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x文件的本地文件列表,每一项是一个 File 对象

tempFiles 的属性描述

名称类型必备默认值兼容性描述
namestring-Web: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x选择的文件名称
pathstring-Web: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x文件路径
sizenumber-Web: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x文件大小,单位 B
typestring-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -选择的文件类型
可选值:
  • 'video': 选择了视频文件;
  • 'image': 选择了图片文件;
  • 'audio': 选择了音频文件;
  • 'file': 选择了除图片和视频的文件; |
type 的属性描述
合法值兼容性描述
videoWeb: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x视频类型
imageWeb: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x图片类型
audioWeb: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x音频类型
fileWeb: 4.0; 微信小程序: 4.41; Android: 4.51; iOS: 4.61; HarmonyOS: x除图片和音视频类型的文件

ChooseFileFail 的属性值 @choosefilefail-values

名称类型必备默认值兼容性描述
errCodenumber-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -错误码
errSubjectstring-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -统一错误主题(模块)名称
dataany-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -错误信息中包含的数据
causeError-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -源错误信息,可以包含多个错误,详见SourceError
errMsgstring-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -

errCode 的属性描述

合法值兼容性描述
1101001Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -用户取消
1101002Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -urls至少包含一张图片地址
1101003Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -文件不存在
1101004Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -图片加载失败
1101005Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -未获取权限
1101006Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -图片或视频保存失败
1101007Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -图片裁剪失败
1101008Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -拍照或录像失败
1101009Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -图片压缩失败
1101010Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -其他错误

Android端返回的路径是content协议。

示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/choose-file/choose-file

appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/choose-file/choose-file

示例

vue
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1;">
  <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>
      <!-- #ifdef APP-IOS -->
      <view style="padding-left: 20px; padding-right: 20px;">
      		<text style="color: #353535; font-size: 15px;">
      			iOS 调试首先需要添加对应 audio、video、image 存储到文件系统中,步骤如下:\n
      			1. 从系统相册或者语音备忘录中选择具体的video、image、audio;\n
      			2. 点击 ‘分享’ 按钮;\n
      			3. 下滑点击 ’存储到”文件“‘按钮;\n
      		</text>
      </view>
      <!-- #endif -->
      <button size="mini" @click="log=''">清空日志</button>
      <text style="margin: 2px; padding: 2px; border: 1px solid #000000;">{{ log }}</text>

      <button class="btnstyle" type="primary" @tap="chooseVideo">选择文件(video) 单选</button>
      <button class="btnstyle" type="primary" @tap="chooseVideoMul">选择文件(video) 多选</button>
      <button class="btnstyle" type="primary" @tap="playVideo">选择文件(video)并播放</button>
      <video class="video" :src="src" :controls="true" :autoplay="true" :loop="true">111</video>
      <button class="btnstyle" type="primary" @tap="chooseImage">选择文件(image) 单选</button>
      <button class="btnstyle" type="primary" @tap="chooseImageMul">选择文件(image) 多选</button>
      <button class="btnstyle" type="primary" @tap="viewImg">选择文件(image) 并预览</button>
      <button class="btnstyle" type="primary" @tap="chooseAudio">选择文件(audio) 单选</button>
      <button class="btnstyle" type="primary" @tap="chooseAudioMul">选择文件(audio) 多选</button>
      <button class="btnstyle" type="primary" @tap="playAudio">选择文件(audio) 并播放</button>
      <button class="btnstyle" type="primary" @tap='chooseAll'>选择文件(all) 单选</button>
      <button class="btnstyle" type="primary" @tap='chooseAllMul'>选择文件(all) 多选</button>

      <view style="height: 4px;"></view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script setup lang="uts">
  const log = ref('')
  const title = ref('choose-file')
  const src = ref('')
  const _audioContext = ref(null as InnerAudioContext | null)

  onUnmounted(() => {
    if (_audioContext.value != null) {
      _audioContext.value!.destroy()
    }
  })

  const getPath = (chooseFils: ChooseFileTempFile[]): string => {
    var urls = new Array<string>()
    chooseFils.forEach(value => {
      urls.push(value.path)
    })
    return urls.join(', ')
  }

  const chooseVideo = () => {
    uni.chooseFile({
      type: 'video',
      count: 1,
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          log.value += getPath(res.tempFiles) + '\n\n'
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }

  const chooseVideoMul = () => {
    uni.chooseFile({
      type: 'video',
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          log.value += getPath(res.tempFiles) + '\n\n'
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }

  const playVideo = () => {
    uni.chooseFile({
      type: 'video',
      count: 1,
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          src.value = res.tempFiles[0].path
          _audioContext.value?.destroy()
          _audioContext.value = null
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }

  const chooseImage = () => {
    uni.chooseFile({
      type: 'image',
      count: 1,
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          log.value += getPath(res.tempFiles) + '\n\n'
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }

  const chooseImageMul = () => {
    uni.chooseFile({
      type: 'image',
      count: 90,
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          log.value += getPath(res.tempFiles) + '\n\n'
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }

  const viewImg = () => {
    uni.chooseFile({
      type: 'image',
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          const tempFiles: Array<string> = res.tempFiles.map((value) => {
          	return value.path
          })
          uni.previewImage({
            current: 0,
            urls: tempFiles,
            success: (res) => {
              console.log(res)
            },
            fail: (err) => {
              console.log(err)
            }
          })
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }

  const chooseAudio = () => {
    uni.chooseFile({
      type: 'audio',
      count: 1,
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          log.value += getPath(res.tempFiles) + '\n\n'
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }

  const chooseAudioMul = () => {
    uni.chooseFile({
      type: 'audio',
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          log.value += getPath(res.tempFiles) + '\n\n'
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }

  const playAudio = () => {
    uni.chooseFile({
      type: 'audio',
      count: 1,
      success: (res) => {
        console.log(res);
        if (res.tempFiles.length > 0) {
          if (_audioContext.value == null) {
          src.value = ''
            _audioContext.value = uni.createInnerAudioContext()
            _audioContext.value!.autoplay = true
          } else if (!_audioContext.value!.paused) {
            _audioContext.value!.stop()
          }
          _audioContext.value!.src = res.tempFiles[0].path
        }
      },
      complete: (res) => {
        console.log(res);
      }
    });
  }

  const chooseAll = () => {
    uni.chooseFile({
      type: 'all',
      count: 1,
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          log.value += getPath(res.tempFiles) + '\n\n'
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }

  const chooseAllMul = () => {
    uni.chooseFile({
      type: 'all',
      success: (res) => {
        console.log(res)
        if (res.tempFiles.length > 0) {
          log.value += getPath(res.tempFiles) + '\n\n'
        }
      },
      complete: (res) => {
        console.log(res)
      }
    })
  }
</script>

<style>
  .btnstyle {
    margin: 4px;
  }

  .video {
    width: 100%;
    height: 225px;
  }

  .uni-uploader__input-box {
    margin: 5px;
    width: 104px;
    height: 104px;
    border: 1px solid #D9D9D9;
  }
</style>

:::

参见

tips

  • chooseFile是系统UI,其风格不同rom可能有差异。多选时有的是长按、有的是checkbox。系统UI的暗黑模式、国际化跟随系统,而不跟随App。
  • chooseFile不仅仅是选择现有文件,有的手机也可以新拍照、拍摄、录音。但不同系统的实现存在差异。

通用类型

GeneralCallbackResult

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -错误信息