Uploader 文件上传 APP
该方法或组件支持移动端小程序调用
PC
该方法或组件支持桌面端小程序调用

用于文件上传。

引入方式

数字生活小程序工程中,不需要 import,直接使用组件标签即可。

import { Uploader } from '@21cnfe/vui';
Vue.component('Uploader', Uploader);

代码演示

基本用法
<template>
<div class="page">
<Uploader
class="uploader"
ref="uploader"
post-action="post.action"
accept="image/*"
v-model="files"
:multiple="true"
:size="1024 * 1024 * 200"
@input-filter="inputFilter"
@input-file="inputFile"
:customAction="customAction"
>
<span class="upload_button">
<b><Icon type="upload" :color="['#FFFFFF']" size="xs"></Icon> 文件上传</b>
</span>
</Uploader>
<ul class="file-list">
<li class="file" v-for="(file, index) in files" :key="index">
<div class="left">
<Icon type="file-image-regular" :color="['#1089ff']" size="lg"></Icon>
</div>
<div class="middle">
<div class="top">
<span class="name">{{ `${file.name}` }}</span>
</div>
<div class="bottom">
<span class="size"> {{ `大小:${file.size}` }}</span>
<span class="speed"> {{ `速度:${file.speed}` }}</span>
</div>
</div>
<div class="right">
<span class="status" v-show="file.success === true">{{ file.success === true ? '成功' : '' }}</span>
<span class="delete" @click="handleDelete(index, true)"><Icon class="icon" type="circle-xmark-solid" :color="['#cccccc']" size="md"></Icon></span>
</div>
</li>
</ul>
<div>
</template>

<script>
export default {
name: 'uploaderExample',
data () {
return {
files: [],
uploadFiles: []
}
},
watch: {
files (val) {
console.log(val)
}
},
methods: {
inputFilter(newFile, oldFile, prevent) {
if (newFile && !oldFile) {
// 添加文件前, 过滤系统文件 和隐藏文件
if (/(\/|^)(Thumbs\.db|desktop\.ini|\..+)$/.test(newFile.name)) {
return prevent();
}
// 过滤 php html js 文件
if (/\.(php5?|html?|jsx?)$/i.test(newFile.name)) {
return prevent();
}
}
},
async inputFile(newFile, oldFile) {
if (newFile && !oldFile) {
// add, 数据处理,添加文件后缀属性
this.$refs.uploader.active = true;
}
if (newFile && oldFile) {
// update
}
if (!newFile && oldFile) {
// remove
}
},
customAction (file) {
this.$Toast.loading({
content: '上传中...',
duration: null,
});
return new Promise(resolve => {
setTimeout(() => {
this.$Toast.hide();
this.$Toast.success({
content: '上传成功',
duration: 1.2,
mask: false
});
this.$refs.uploader.update(file, {
success: true,
file: file.file,
size: file.size,
type: file.type
});
resolve(true);
}, 1000);
})
}
}
}
</script>

属性

属性 说明 类型 默认值
input-id input 标签的 id 属性 String this.name
name input标签的 name 属性 String file
post-action POST 请求的上传URL String undefined
put-action PUT 请求的上传URL String undefined
custom-action 自定义上传方法,优先级高于 put-action, post-action async Function undefined
headers 自定义上传请求 header 数据 Object {}
data POST 请求: 附加请求的 body,PUT 请求: 附加请求的 query Object {}
value, v-model 文件列表,不可直接修改 files,请使用 add, update, remove 方法修改 Array []
accept 表单的accept属性, MIME type String undefined
multiple 文件表单的 multiple 属性,是否允许选择多个文件 Boolean false
directory 文件表单的 directory 属性,是否是上传文件夹 Boolean false
extensions 允许上传的文件后缀 Array/String/RegExp undefined
size 允许上传的最大字节 Number 0
timeout 上传超时时间毫秒 Number 0
maximum 列表最大文件数 Number props.multiple ? 0 : 1
thread 同时并发上传的文件数量线程数 Number 1
drop 拖拽上传 Boolean false
drop-directory 是否开启拖拽目录 Boolean true

事件

事件名称 说明 参数
@input 文件选择事件 `files: Array<File
@input-filter 输入文件过滤 `newFile: File
@input-file 添加,更新,移除 后 `newFile: File

方法

方法名 说明 参数 结果
get() 使用id获得某个对象 id 存在返回文件对象否则返回 false
add() 添加一个或多个文件 files, index 传入的是数组返回数组否则对象或false
update() 更新某个对象 id, 更新的数据对象 成功返回 newFile 失败返回 false
remove() 移除某个文件对象 id 成功返回 oldFile 失败返回 false
replace() 替换两个文件的位置 id1, id2 成功返回 oldFile 失败返回 false
clear() 清空文件列表 `` 总是返回 true

实例 File

名称 说明 类型 默认值
fileObject 是否是文件类型 Boolean true
id 文件id String Number
size 文件大小 Number -1
name 文件名 String ``
type MIME类型 String ``
active 激活或终止上传 Boolean false
error 上传失败错误代码 String ``
success 是否上传成功 Boolean false
putAction 自定义当前文件 POST 地址 String ``
headers 自定义当前文件 HTTP Header Object ``
data 自定义当前文件 body 或 query 附加内容 Object ``
timeout 自定义当前单个文件的上传超时时间 Number ``
response 响应的数据 Object String
progress 上传进度 String 0.00
speed 每秒的上传速度 Number 0
xhr HTML5 上传 XMLHttpRequest 对象 XMLHttpRequest undefined
iframe HTML4 上传 iframe 元素 Element undefined
  1. 1. Uploader 文件上传 APP 该方法或组件支持移动端小程序调用 PC 该方法或组件支持桌面端小程序调用
    1. 1.1. 引入方式
    2. 1.2. 代码演示
    3. 1.3. 属性
    4. 1.4. 事件
    5. 1.5. 方法
    6. 1.6. 实例 File