文件需要上传?推荐uni-cdn,帮你节省至少30%的 CDN 费用!详情

# uni.chooseFile(OBJECT)

Select files from the local directory.

This API is mainly used to select non-media files. For media files, there are three specialized APIs:

Platform difference description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
x (HBuilder X2.9.9+) x(wx.chooseMessageFile can be used) x x x x x x
  • If you need to select non-media files on the App side, you can search for [File Selection] in the plugin market (https://ext.dcloud.net.cn/search?q=%E6%96%87%E4%BB%B6%E9 %80%89%E6%8B%A9), in which Native.js can be used on the Android side without native plugins, while the iOS side requires native plugins.
  • If you want to download files to the plus.io-controlled directories such as _doc, _downloads, _documents, etc. on the App side, you can use [plus.io Api](https://www.html5plus.org/doc /zh_cn/io.html), make the selection box yourself.
  • Most of the files are selected for uploading. uni ui encapsulates a more complete uni-file-picker component. Files are selected and uploaded to uniCloud's free storage and cdn for one-stop integration. Highly recommended.

OBJECT parameter description

Parameter name Type Defaults Required Instruction Platform difference description
count Number 100 No Maximum number of files that can be selected See the instructions below
type String 'all' No Type of selected file See the instructions below
extension Array<String> No Filter by file extension. No empty string is allowed in any item. No filtering by default. See the instructions below
sourceType Array<String> ['album','camera'] No (Available only when the type is image or video) album refers to selecting images from album, and camera refers to using camera. Both exist by default. If you want to open the camera or select from the album directly, please use only one option.
success Function Yes If success, it returns the list of local file paths of the image tempFilePaths
fail Function No Callback function for failed interface calling
complete Function No Callback function for closed interface calling (available both for successful and failed calling)

Tips

  • The performance of the count value on H5 platform is based on the specification of the browser. As seen from the resent testing result, only single/multiple selection can be specified, and the amount limitation is unavailable. Moreover, there are few real mobile browsers that support multiple choices.
  • The sourceType value behaves differently on the H5 platform depending on the browser. Generally, it is not restricted to use only the photo album, and some browsers cannot restrict the use of the camera.
  • extension only supports file extensions temporarily, such as ['.zip','.exe','.js'], instead of application/msword and similar values

注:文件的临时路径,在应用本次启动期间可以正常使用,如需持久保存,需在主动调用 uni.saveFile,在应用下次启动时才能访问得到。

Legal values of OBJECT.type.

Value Instruction
all Select from all files
video Only video files can be selected
image Only image files can be selected

Tips

  • If both the type attribute and the extension exist, such as {type:'image',extension:['.png','.jpg']}, the image/png,image/jpg file will be selected
  • If only the extension attribute is configured, such as {extension:['.doc','.xlsx','.docx']}, the .doc,.xlsx,.docx file will be selected. See accept attribute for details
  • In the WeChat environment, if type="all", the extension attribute is invalid

success return parameter description

Parameter Type Instruction
tempFilePaths Array<String> List of local file paths for files.
tempFiles Array<Object>, Array<File> List of local files. Each item is a file object.

The structure of File object is as follows.

Parameter Type Instruction
path String Local file path
size Number Local file size, in: B
name String File names with extensions, only supported by H5.
type String Types of files, only supported by H5.

Example

uni.chooseFile({
  count: 6, //默认100
  extension:['.zip','.doc'],
	success: function (res) {
		console.log(JSON.stringify(res.tempFilePaths));
	}
});

//Select image file
uni.chooseFile({
  count: 10,
  type: 'image',
  success (res) {
    // tempFilePath can display images as the src attribute of the img tags.
    const tempFilePaths = res.tempFiles
  }
})

# wx.chooseMessageFile(OBJECT)

Select a file from a WeChat chat session.

Platform difference description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
x x (base library 2.5.0+) x x x (base library 1.18.0+) x x