# uni.chooseFile(options)

从本地选择文件

# chooseFile 兼容性

Web 微信小程序 Android iOS
4.0 4.41 4.51 x

# 参数

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

# ChooseFileSuccess 的属性值

名称 类型 必备 默认值 兼容性 描述
tempFilePaths Array<string> - - 文件的本地文件路径列表, Android平台不支持
tempFiles Array<ChooseFileTempFile> - - 文件的本地文件列表,每一项是一个 File 对象
名称 类型 必备 默认值 兼容性 描述
name string - - 选择的文件名称
path string - - 文件路径
size number - - 文件大小,单位 B
type string - - 选择的文件类型
可选值:
- 'video': 选择了视频文件;
- 'image': 选择了图片文件;
- 'audio': 选择了音频文件;
- 'file': 选择了除图片和视频的文件;
合法值 兼容性 描述
video - -
image - -
audio - -
file - -

# IMediaError 的属性值

名称 类型 必备 默认值 兼容性 描述
errCode number - - 错误码
合法值 兼容性 描述
1101001 - 用户取消
1101002 - urls至少包含一张图片地址
1101003 - 文件不存在
1101004 - 图片加载失败
1101005 - 未获取权限
1101006 - 图片或视频保存失败
1101007 - 图片裁剪失败
1101008 - 拍照或录像失败
1101009 - 图片压缩失败
1101010 - 其他错误
errSubject string - - 统一错误主题(模块)名称
data any - - 错误信息中包含的数据
cause Error - - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - - -

# 示例

hello uni-app x

扫码体验(手机浏览器跳转到App直达页)

Template

Script

<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1;">
  <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>

      <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"></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>



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

  .video {
    align-self: center;
    width: 300px;
    height: 225px;
  }

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

# 参见

# 示例

hello uni-app x

扫码体验(手机浏览器跳转到App直达页)

Template

Script

<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1;">
  <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>

      <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"></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>



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

  .video {
    align-self: center;
    width: 300px;
    height: 225px;
  }

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

# tips

  • chooseFile在App平台是系统UI,其风格不同rom可能有差异。多选时有的是长按、有的是checkbox。系统UI的暗黑模式、国际化跟随系统,而不跟随App。
  • Android平台的chooseFile可以选择录音机,新录一段声音。此时可用于替代录音API uni.getRecorderManager()

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息