您现在的位置是:网站首页> 编程资料编程资料
elementui使用el-upload组件如何实现自定义上传_vue.js_
2023-05-24
356人已围观
简介 elementui使用el-upload组件如何实现自定义上传_vue.js_
使用el-upload组件实现自定义上传
方式一:选择后自动上传
使用 http-request 覆盖默认的上传行为,可以自定义上传的实现
利用 before-upload 上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise且被 reject,则停止上传
template 部分
部署流程定义
js 部分
beforeUpload (file) { // 上传文件之前钩子 const type = file.name.split('.')[1] if (type !== 'bpmn') { this.$message({ type: 'error', message: '只支持bpmn文件格式!' }) return false } }, uploadBpmn (param) { // 部署流程定义(点击按钮,上传bpmn文件,上传成功后部署,然后重新加载列表) const formData = new FormData() formData.append('processDefinition', param.file) // 传入bpmn文件 this.$API({ name: 'deploy', data: formData, headers: {'Content-Type': 'multipart/form-data'} }).then(res => { if (res.data.code == 0) { this.$message({ type: 'success', message: res.data.msg }) } else { this.$message({ type: 'error', message: res.data.msg }) } }).catch(error => { this.$message({ type: 'error', message: error }) }).finally(() => { this.getList() }) },如果不想上传成功后显示上传文件列表,可以隐藏掉文件列表
可以在组件中设置 :show-file-list="false"
或者
::v-deep .el-upload-list { display: none !important; }方式二:选择后手动上传
上传文件 将文件拖到此处,或点击上传确 定
使用el-upload上传文件夹
只需要为 input 输入框设置 webkitdirectory 属性
mounted() { if (this.$route.query.type === 'folder') { this.$nextTick(() => { document.querySelector('.el-upload__input').webkitdirectory = true }) } },封装elementui el-upload文件上传组件
// 自定义的全局组件my-component let _utils = new Utils() Vue.component( 'uploadfile', { props: { uploaddata: { type: Object } }, template: `表名称:{{uploaddata.sheetname}}选择文件 `, data() { return {} }, methods: { /* 上传文件之前的钩子,参数为上传的文件, 若返回 false 或者返回 Promise 且被 reject,则停止上传。 */ beforeUpload(file){ const fileSuffix = file.name.substring(file.name.lastIndexOf(".")+1); const whiteList = ["xls", "xlsx"]; if (whiteList.indexOf(fileSuffix) === -1) { _utils.MessageError(this,"上传文件只能是xls、xlsx格式") return false; } const isLt2M = file.size / 1024 / 1024 < 5; if (!isLt2M) { _utils.MessageError(this,"上传文件大小不能超过5MB") return false; } }, handleRemove(file, fileList) { }, handleSuccess(response, file, fileList) { let {code, msg} = response if (code == 0) { utils.MessageSuccess(this, "文件上传成功") } else { utils.MessageError(this, msg) } }, /* 点击文件列表中已上传的文件时的钩子 */ handlePreview(file) { // console.log(file); }, /* 文件超出个数限制时的钩子 */ handleExceed(files, fileList) { }, /* 删除文件之前的钩子,参数为上传的文件和文件列表, 若返回 false 或者返回 Promise 且被 reject,则停止删除 */ beforeRemove(file, fileList) { // return this.$confirm(`确定移除 ${file.name}?`); } } } ) 以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
您可能感兴趣的文章:
相关内容
- 微信小程序实现分页查询详解_javascript技巧_
- vue响应式Object代理对象的修改和删除属性_vue.js_
- Vue数据更新视图不更新的几种解决方案小结_vue.js_
- vue3响应式Object代理对象的读取示例详解_vue.js_
- Antd ProComponents中的EditableProTable无法在子行继续新增子行的解决方案_javascript技巧_
- 带你领略Object.assign()方法的操作方式_javascript技巧_
- node制作一个视频帧长图生成器操作分享_node.js_
- vue选择下拉框动态变化表单方式_vue.js_
- Javascript对象及Proxy工作原理详解_vue.js_
- vue2中如何自定义组件的v-model_vue.js_
