English
文件需要上传?推荐
uni-cdn
,帮你节省至少30%的 CDN 费用!详情。
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 |
_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.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
['.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
{type:'image',extension:['.png','.jpg']}
, the image/png,image/jpg
file will be selected{extension:['.doc','.xlsx','.docx']}
, the .doc,.xlsx,.docx
file will be selected. See accept attribute
for detailstype="all"
, the extension
attribute is invalidsuccess 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
}
})
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 |