readme.md 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

## FilePicker 文件选择上传

> **组件名:uni-file-picker**
>  代码块: `uFilePicker`


文件选择上传组件,可以选择图片、视频等任意文件并上传到当前绑定的服务空间

> **注意事项**
> 为了避免错误使用,给大家带来不好的开发体验,请在使用组件前仔细阅读下面的注意事项,可以帮你避免一些错误。
> - 组件需要依赖 `sass` 插件 ,请自行手动安装
> - 使用组件需要绑定服务空间
> - 选择文件目前只支持 `H5` 和 `微信小程序平台` ,且 `微信小程序平台` 使用 `wx.chooseMessageFile()`
> - 如使用过程中有任何问题,或者您对uni-ui有一些好的建议,欢迎加入 uni-ui 交流群:871950839



## API

### FilePicker Props

| 属性名					| 类型			| 默认值	| 可选值								| 说明																	|
| :-:						| :-:			| :-:		| :-:									| :-:																	|
| v-model/value	| Array\Object	| -			| -						| 组件数据,通常用来回显 ,类型由`return-type`属性决定	,**格式见下文**	|
| disabled			| Boolean	| false	| -										| 组件禁用								|
| readonly			| Boolean	| false	| -										| 组件只读,不可选择,不显示进度,不显示删除按钮								|
| return-type		| String	| array	| array/object				| 限制 `value` 格式,当为 `object`	 时	,组件只能单选,且会覆盖	|
| disable-preview| Boolean	| false	| -									| 禁用图片预览,仅	`mode:grid`生效	|
| del-icon			| Boolean	| true	| -										| 是否显示删除按钮	|
| auto-upload		| Boolean	| true	| -										| 是否自动上传,值为`true`则只触发@select,可自行上传|
| limit					| Number\String	| 9	| -									| 最大选择个数 ,h5 会自动忽略多选的部分			|
| title					| String	| -			| -										| 组件标题,右侧显示上传计数			|
| mode					| String	| list	| list/grid						| 选择文件后的文件列表样式								|
| file-mediatype| String	| image	| image/video/all			| 选择文件类型,all 只支持 H5 和微信小程序平台	|
| file-extname	| Array\String		| -			| -						| 选择文件后缀,字符串的情况下需要用逗号分隔(推荐使用字符串),根据 `file-mediatype` 属性而不同|
| list-styles	  | Object	| -			| -										| `mode:list` 时的样式			|
| image-styles	| Object	| -			| -										| `mode:grid` 时的样式	|


### value 格式 

三个属性必填,否则影响组件显示

```json 
[
	{
		"name":"file.txt",
		"extname":"txt",
		"url":"https://xxxx",
		// ...
	}
]

```

### list-styles 格式 

```json 
{
	"borderStyle":{
		"color":"#eee",		// 边框颜色
		"width":"1px",		// 边框宽度
		"style":"solid", 	// 边框样式
		"radius":"5px" 		// 边框圆角,不支持百分比
	},
	"border":false, // 是否显示边框
	"dividline":true // 是否显示分隔线
}
```

### image-styles 格式 

```json 
{
	"height": 60,	// 边框高度
	"width": 60,	// 边框宽度
	"border":{ // 如果为 Boolean 值,可以控制边框显示与否
		"color":"#eee",		// 边框颜色
		"width":"1px",		// 边框宽度
		"style":"solid", 	// 边框样式
		"radius":"50%" 		// 边框圆角,支持百分比
	}
}
```

### FilePicker Events

|事件称名		|说明							|	返回值	|					
|:-:			|:-:							|	:-:	|
|@select	| 选择文件后触发 		| 见下文|
|@progress|文件上传时触发			| 见下文|
|@success	|上传成功触发				| 见下文|
|@fail		|上传失败触发				| 见下文|
|@delete	|文件从列表移除时触发| 见下文|


#### Callback Params

```json
{
	"progress"			: Number, 		// 上传进度 ,仅 @progress 事件包含此字段
	"index"				: Number, 		// 上传文件索引 ,仅 @progress 事件包含此字段
	"tempFile"			: file, 		// 当前文件对象 ,包含文件流,文件大小,文件名称等,仅 @progress 事件包含此字段
	"tempFiles"			: files, 		// 文件列表,包含文件流,文件大小,文件名称等
	"tempFilePaths"		: filePaths, 	// 文件地址列表,@sucess 事件为上传后的线上文件地址
}

```


### FilePicker Methods

通过 `$ref` 调用

| 方法称名						| 说明			|		参数 		|				
| :-:								| :-:		  |		:-:		  |				
| upload						| 手动上传 	,如`autoUpload``false`  ,必须调用此方法| - |

### FilePicker Slots

插槽可定义上传按钮显示样式 

|	插槽名 	| 说明 |
| :-:			| :-:		  |
|	default	|默认插槽|

## 组件用法

### 基础用法

```html
<uni-file-picker 
	v-model="imageValue" 
	fileMediatype="image" 
	mode="grid" 
	@select="select" 
	@progress="progress" 
	@success="success" 
	@fail="fail" 
/>
```

```javascript
export default {
		data() {
			return {
				imageValue:[]
			}
		},
		methods:{
			// 获取上传状态
			select(e){
				console.log('选择文件:',e)
			}
			// 获取上传进度
			progress(e){
				console.log('上传进度:',e)
			},
			
			// 上传成功
			success(e){
				console.log('上传成功')
			},
			
			// 上传失败
			fail(e){
				console.log('上传失败:',e)
			}
		}
}

```

### 选择指定后缀图片,且限制选择个数

配置 `file-mediatype` 属性为 `image`,限定只选择图片

配置 `file-extname` 属性为 `'png,jpg'`,限定只选择 `png``jpg`后缀的图片

配置 `limit` 属性为 1 ,则最多选择一张图片

配置 `mode` 属性为 `grid` ,可以使用九宫格样式选择图片


```html
<uni-file-picker 
	v-model="imageValue"  
	file-mediatype="image"
	mode="grid"
	file-extname="png,jpg"
	:limit="1"
	@progress="progress" 
	@success="success" 
	@fail="fail" 
	@select="select"
/>
```

### 手动上传

配置 `auto-upload` 属性为 `false` ,可以停止自动上传,通过`ref`调用`upload`方法自行选择上传时机

```html
<view>
	<uni-file-picker  ref="files" :auto-upload="false"/>
	<button @click="upload">上传文件</button>
</view>
```

```javascript
export default {
		data() {},
		methods:{
			upload(){
				this.$refs.files.upload()
			}
		}
}

```

### 单选图片且点击再次选择

配置 `disable-preview` 属性为 `true`,禁用点击预览图片功能

配置 `del-icon` 属性为 `false`,隐藏删除按钮

配置 `return-type` 属性为 `object`,设置 `value` 类型 ,如需绑定 `array`类型 ,则设置`limit:1`,可达到一样的效果



```html
<uni-file-picker 
	disable-preview
	:del-icon="false"
	return-type="object"
>选择头像</uni-file-picker>
```

### 自定义样式

配置 `image-styles` 属性,可以自定义`mode:image`时的回显样式

配置 `list-styles` 属性,可以自定义`mode:video|| mode:all`时的回显样式

```html
<view>
	<uni-file-picker fileMediatype="image" :image-styles="imageStyles"/>
	<uni-file-picker fileMediatype="all" :list-styles="listStyles"/>
</view>
```

```javascript
export default {
		data() {
			imageStyles:{
				width:64,
				height:64,
				border:{
					color:"#ff5a5f",
					width:2,
					style:'dashed',
					radius:'2px'
				}
			},
			listStyles:{
				// 是否显示边框
				border: true,
				// 是否显示分隔线
				dividline: true,
				// 线条样式
				borderStyle: {
					width:1,
					color:'blue',
					radius:2
				}
			}
		}
}

```



### 使用插槽 

使用默认插槽可以自定义选择文件按钮样式

```html
<uni-file-picker 
	v-model="value" file-mediatype="all">
	<button>选择文件</button>
</uni-file-picker>
```