简体中文
FxUpload
约 2490 字大约 8 分钟
2025-12-16
通过点击或者拖拽上传文件
点击上传
<fx-upload
class="upload-demo"
:accept="accept"
:max-size="maxSize"
:multiple="multiple"
:limit="limit"
:name="name"
:file-list="fileList"
:on-preview="handlePreview"
:on-remove="handleRemove"
:on-success="handleSuccess"
:on-error="handleError"
:on-progress="handleProgress"
:on-change="handleChange"
:before-remove="beforeRemove"
:on-exceed="handleExceed"
>
<fx-button size="small" type="primary">点击上传</fx-button>
<div slot="tip" class="el-upload__tip" v-if="maxSize">只能上传jpg/png文件,且不超过{{maxSize}}kb</div>
<!-- <div slot="file" slot-scope="{file}">
<i class="el-icon-edit"></i>
</div> -->
</fx-upload>
<!-- <fx-button @click="click">button</fx-button> -->
<script>
export default {
data() {
return {
fileList:[],
fileList0: [{ name: 'food.jpeg',size:1000, url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' },{ name: 'food.jpeg',status:'uploading',percentage:'50', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }],
multiple: true,
limit: 2,
name:'test',
maxSize:10000,
accept:'.png,.jpg'
};
},
methods: {
handleSuccess(response, file, fileList) {
console.log('handleSuccess', response, file, fileList);
},
handleError(err, file, fileList) {
console.log('handleError', err, file, fileList);
},
handleProgress(event, file, fileList) {
console.log('handleProgress', event, file, fileList);
},
handleChange(file, fileList) {
console.log('handleChange', file, fileList,this.fileList);
},
handlePreview(file) {
console.log('handlePreview', file);
},
handleRemove(file, fileList) {
console.log('handleRemove', file, fileList);
},
beforeRemove(file, fileList) {
console.log('beforeRemove', file, fileList);
console.log(this.fileList);
},
handleExceed(file, fileList, msg) {
console.log('handleExceed', file, fileList, msg);
},
beforeUpload(){
return false;
},
file() {
console.log(arguments);
},
remove() {
console.log(arguments);
},
},
};
</script>用户头像上传
使用 before-upload 限制用户上传的图片格式和大小。
<fx-upload
class="avatar-uploader"
:max-size="maxSize"
:show-file-list="showFileList"
:on-success="handleAvatarSuccess"
:on-error="handleAvatarError"
:before-upload="beforeAvatarUpload"
>
<img v-if="imageUrl" :src="imageUrl" class="avatar" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
<div slot="tip" class="el-upload__tip" v-if="maxSize">只能上传图片文件,且不超过{{maxSize}}kb</div>
</fx-upload>
<script>
export default {
data() {
return {
imageUrl: '',
showFileList:false,
maxSize:20
};
},
methods: {
handleAvatarSuccess(res, file) {
console.log('handleAvatarSuccess...',res,file)
this.imageUrl = URL.createObjectURL(file.raw);
},
handleAvatarError(err,file){
console.log('handleAvatarError...',err,file)
this.imageUrl = URL.createObjectURL(file.raw);
},
beforeAvatarUpload(files) {
const file=files[0];
const isJPG = file.type === 'image/jpeg';
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上传头像图片只能是 JPG 格式!');
}
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 2MB!');
}
return isJPG && isLt2M;
},
},
};
</script>
<style>
.avatar-uploader .el-upload {
border: 1px solid #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #409eff;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 70px;
height: 70px;
line-height: 70px;
text-align: center;
}
.avatar {
width: 70px;
height: 70px;
display: block;
}
</style>照片墙
使用 list-type 属性来设置文件列表的样式。
<fx-upload
url="https://jsonplaceholder.typicode.com/posts/"
list-type="picture-card"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove">
<i class="el-icon-plus"></i>
</fx-upload>
<fx-dialog :visible.sync="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt="">
</fx-dialog>
<script>
export default {
data() {
return {
dialogImageUrl: '',
dialogVisible: false
};
},
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
}
}
}
</script>文件缩略图
使用 scoped-slot 去设置缩略图模版。
<fx-upload
url="#"
list-type="picture-card"
:auto-upload="false">
<i slot="default" class="el-icon-plus"></i>
<div slot="file" slot-scope="{file}">
<img
class="el-upload-list__item-thumbnail"
:src="file.url" alt=""
>
<span class="el-upload-list__item-actions">
<span
class="el-upload-list__item-preview"
@click="handlePictureCardPreview(file)"
>
<i class="el-icon-zoom-in"></i>
</span>
<span
v-if="!disabled"
class="el-upload-list__item-delete"
@click="handleRemove(file)"
>
<i class="el-icon-delete"></i>
</span>
</span>
</div>
</fx-upload>
<fx-dialog :visible.sync="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt="">
</fx-dialog>
<script>
export default {
data() {
return {
dialogImageUrl: '',
dialogVisible: false,
disabled: false
};
},
methods: {
handleRemove(file) {
console.log(file);
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
handleDownload(file) {
console.log(file);
}
}
}
</script>图片列表缩略图
<fx-upload
class="upload-demo"
url="https://jsonplaceholder.typicode.com/posts/"
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
list-type="picture">
<fx-button size="small" type="primary">点击上传</fx-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</fx-upload>
<script>
export default {
data() {
return {
fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
};
},
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
}
}
}
</script>上传文件列表控制
通过 on-change 钩子函数来对列表进行控制
<fx-upload
class="upload-demo"
url="https://jsonplaceholder.typicode.com/posts/"
:on-change="handleChange"
:file-list="fileList">
<fx-button size="small" type="primary">点击上传</fx-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</fx-upload>
<script>
export default {
data() {
return {
fileList: [{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
}, {
name: 'food2.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
}]
};
},
methods: {
handleChange(file, fileList) {
this.fileList = fileList.slice(-3);
}
}
}
</script>拖拽上传
<fx-upload
class="upload-demo"
drag
url="https://jsonplaceholder.typicode.com/posts/"
multiple>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
</fx-upload>手动上传
<fx-upload
class="upload-demo"
ref="upload"
url="https://jsonplaceholder.typicode.com/posts/"
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
:auto-upload="false">
<fx-button slot="trigger" size="small" type="primary">选取文件</fx-button>
<fx-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</fx-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</fx-upload>
<script>
export default {
data() {
return {
fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
};
},
methods: {
submitUpload() {
this.$refs.upload.submit();
},
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
}
}
}
</script>分片上传
<fx-upload
ref="el"
multiple
:url="url"
:when-chunks="whenChunks"
:chunk="chunk"
:on-progress="onProgress"
:on-success="onSuccess"
:on-error="onError"
:on-exceed="onExceed">
<fx-button size="small" type="primary">点击上传</fx-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</fx-upload>
<script>
export default {
data(){
return {
url:'/mock/uploading.json',
whenChunks:10240,//10k
chunk:{
mime: "multipart/form-data",
size: 5120,
tryTime: 1,
starturl: "/mock/uploadstart.json",
uploadurl: "/mock/uploading.json",
doneurl: "/mock/uploadend.json"
}
}
},
methods:{
onExceed(){
console.log('upload_onExceed',arguments);
},
onProgress(){
console.log('upload_onProgress',arguments);
},
onSuccess(){
console.log('upload_onSuccess',arguments);
},
onError(){
console.log('upload_onError',arguments);
},
onEnd(){
console.log('upload_onEnd',arguments);
}
}
};
</script>Attributes
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| url | 文件上传地址(非切片) | string | — | "/FSC/EM/File/UploadByForm" |
| headers | 设置上传的请求头部 | object | — | — |
| multiple | 是否支持多选文件 | boolean | — | — |
| data | 上传时附带的额外参数 | object | — | — |
| name | 上传的文件字段名 | string | — | file |
| with-credentials | 支持发送 cookie 凭证信息 | boolean | — | false |
| show-file-list | 是否显示已上传文件列表 | boolean | — | true |
| drag | 是否启用拖拽上传 | boolean | — | false |
| accept | 接受上传的文件类型(thumbnail-mode 模式下此参数无效),多个类型逗号分隔 | string | .txt,.pdf,.jpg | — |
| on-preview | 点击文件列表中已上传的文件时的钩子 | function(file) | — | — |
| on-remove | 文件列表移除文件时的钩子 | function(file, fileList) | — | — |
| on-success | 文件上传成功时的钩子 | function(response, file, fileList) | — | — |
| on-error | 文件上传失败时的钩子 | function(err, file, fileList) | — | — |
| on-progress | 文件上传时的钩子 | function(event, file, fileList) | — | — |
| on-change | 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用 | function(file, fileList) | — | — |
| on-exceed | 文件超出限制时的钩子, | function(files, fileList,o) files:新选择的文件 o.type: 1:文件个数超出限制(必须设置limit), 2:文件大小超出限制(必须设置maxSize) 3:文件类型不符合(必须设置accept) | — | — |
| before-upload | 上传文件之前的钩子,参数为上传的文件,若返回 false,则停止上传。 | function(files) | — | — |
| before-remove | 删除文件之前的钩子,参数为上传的文件和文件列表,若返回 false 或者返回 Promise 且被 reject,则停止上传。 | function(file, fileList) | — | — |
| list-type | 文件列表的类型 | string | text/picture/picture-card | text |
| auto-upload | 是否在选取文件后立即进行上传 | boolean | — | true |
| file-list | 上传的文件列表, 例如: [{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}] | array | — | [] |
| http-request | 覆盖默认的上传行为,可以自定义上传的实现 | function(options) | — | — |
| disabled | 是否禁用 | boolean | — | false |
| limit | 最大允许上传个数 | number | — | — |
| max-size | 最大允许上传大小(KB),默认10M | number | — | 10485760 |
| basePath | 上传接口根地址 | string | — | — |
| whenChunks | 在文件大小超过多少时采用切片上传,默认0:不分片,单位B | number | — | 0 |
| chunk | 切片信息,见下面chunk表 | object | — | — |
chunk
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| mime | 请求的Content-Type的值 | string | — | — |
| size | 切片大小,单位B | number | — | 2097152 |
| tryTime | 失败重试次数 | number | — | 3 |
| starturl | 上传预处理接口 | string | — | "/FSC/EM/File/ChunkFileUploadStart" |
| uploadurl | 上传切片接口 | string | — | "/FSC/EM/File/ChunkFileUploadDataByStream" |
| doneurl | 上传完成后处理接口 | string | — | "/FSC/EM/File/ChunkFileUploadComplete" |
Slot
| 方法名 | 说明 |
|---|---|
| trigger | 触发文件选择框的内容 |
| tip | 提示说明文字 |
Methods
| 方法名 | 说明 | 参数 |
|---|---|---|
| clearFiles | 清空已上传的文件列表(该方法不支持在 before-upload 中调用) | — |
| getFiles | 获取已上传的文件列表 | — |
| abort | 取消上传请求 | ( file: fileList 中的 file 对象 ) |
| uploadFile | 手动上传文件(列表),可用于重新上传 | file |
