el-upload 上传文件和上传图片的基本用法
el-upload 上传excel
<template>
<el-form :model="form">
<el-form-item label="上传文件" :label-width="formLabelWidth">
<el-upload
ref="uploadExcel"
action="https://jsonplaceholder.typicode.com/posts/"
:limit=limitNum
:auto-upload="false"
accept=".xlsx"
:before-upload="beforeUploadFile"
:on-change="fileChange"
:on-exceed="exceedFile"
:on-success="handleSuccess"
:on-error="handleError"
:file-list="fileList">
<el-button size="small" plain>选择文件</el-button>
<div slot="tip" class="el-upload__tip">只能上传xlsx(Excel2007)文件,且不超过10M</div>
</el-upload>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" @click="uploadFile">立即上传</el-button>
<el-button size="small">取消</el-button>
</el-form-item>
</el-form>
</template> <script>
import axios from 'axios'
export default {
data() {
return {
limitNum: 1,
formLabelWidth: '80px',
form: {
file: ''
},
fileList: []
}
},
methods: {
// 文件超出个数限制时的钩子
exceedFile(files, fileList) {
this.$notify.warning({
title: '警告',
message: `只能选择 ${this.limitNum} 个文件,当前共选择了 ${files.length + fileList.length} 个`
});
},
// 文件状态改变时的钩子
fileChange(file, fileList) {
console.log('change')
console.log(file)
this.form.file = file.raw
console.log(this.form.file)
console.log(fileList)
},
// 上传文件之前的钩子, 参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传
beforeUploadFile(file) {
console.log('before upload')
console.log(file)
let extension = file.name.substring(file.name.lastIndexOf('.')+1)
let size = file.size / 1024 / 1024
if(extension !== 'xlsx') {
this.$notify.warning({
title: '警告',
message: `只能上传Excel2017(即后缀是.xlsx)的文件`
});
}
if(size > 10) {
this.$notify.warning({
title: '警告',
message: `文件大小不得超过10M`
});
}
},
// 文件上传成功时的钩子
handleSuccess(res, file, fileList) {
this.$notify.success({
title: '成功',
message: `文件上传成功`
});
},
// 文件上传失败时的钩子
handleError(err, file, fileList) {
this.$notify.error({
title: '错误',
message: `文件上传失败`
});
},
uploadFile() {
this.$refs.uploadExcel.submit()
/*
let formData = new FormData()
formData.append('file', this.form.file)
axios.post('https://jsonplaceholder.typicode.com/posts/',
formData,
{ "Content-Type": "multipart/form-data" }
)
.then(res => {
console.log('res')
console.log(res)
})
.catch(err => { })
*/
}
}
}
</script> <style lang="scss" scoped> </style>

el-upload 上传图片
<template>
<el-form :model="form">
<el-form-item label="上传图片" :label-width="formLabelWidth">
<el-upload
ref="upload"
action="#"
accept="image/png,image/gif,image/jpg,image/jpeg"
list-type="picture-card"
:limit=limitNum
:auto-upload="false"
:on-exceed="handleExceed"
:before-upload="handleBeforeUpload"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove">
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" @click="uploadFile">立即上传</el-button>
<el-button size="small">取消</el-button>
</el-form-item>
</el-form>
</template> <script>
export default {
data() {
return{
dialogImageUrl: '',
dialogVisible: false,
formLabelWidth: '80px',
limitNum: 3,
form: {}
}
},
methods: {
// 上传文件之前的钩子
handleBeforeUpload(file){
console.log('before')
if(!(file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/jpg' || file.type === 'image/jpeg')) {
this.$notify.warning({
title: '警告',
message: '请上传格式为image/png, image/gif, image/jpg, image/jpeg的图片'
})
}
let size = file.size / 1024 / 1024 / 2
if(size > 2) {
this.$notify.warning({
title: '警告',
message: '图片大小必须小于2M'
})
}
},
// 文件超出个数限制时的钩子
handleExceed(files, fileList) { },
// 文件列表移除文件时的钩子
handleRemove(file, fileList) {
console.log(file, fileList);
},
// 点击文件列表中已上传的文件时的钩子
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
uploadFile() {
this.$refs.upload.submit()
}
}
}
</script> <style lang="scss" scoped> </style>

el-upload 上传文件和上传图片的基本用法的更多相关文章
- Struts Upload上传文件
1.Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.te ...
- element-ui upload上传文件并携带参数 使用formData对象
需求:上传文件的时候,需要携带其他的参数 问题:使用upload上传文件时,必须使用formData对象,而其他的参数通过data获取的到的,formData和data是不能同时传输的 解决:获取到的 ...
- Django上传文件和上传图片(不刷新页面)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- thinkphp Upload上传文件在客户端生成的临时文件$_FILES['file']['tmp_name']
1.关于thinkphp 的Upload的$_FILES['file']['tmp_name'] 在使用thinkphp上传图片的时候,在上传的$_FILES数组中,有一个$_FILES['file' ...
- Element-UI中Upload上传文件前端缓存处理
Element-UI对于文件上传组件的功能点着重于文件传递到后台处理,所以要求action为必填属性.但是如果需要读取本地文件并在前端直接处理,文件就没有必要传递到后台,比如在本地打开一个JSON文件 ...
- chome(谷歌浏览器)上传文件崩溃/上传图片崩溃/打开浏览文件未响应 解决方案
测试解决方案:关闭搜狗输入法(我用的是搜狗输入法,若使用其他输入法,此方案也可能适用),再测试是否重现浏览器崩溃问题 可选解决方案:升级搜狗输入法(如果想 卸载输入法 也可以) 前面有段时间chome ...
- web 表单方式上传文件方法(不用flash插件)
原理:使用表单的input type="file"标签,通过ajax提交表单请求,后台获取请求中的文件信息,进行文件保存操作 由于我测试用的做了一个上传文件和上传图片方法,所以我有 ...
- ajax如何上传文件(整理)
ajax如何上传文件(整理) 一.总结 一句话总结:用FormData,FormData+ajax=异步上传二进制文件 <form enctype="multipart/form-da ...
- jquery.uploadify上传文件配置详解(asp.net mvc)
页面源码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
随机推荐
- Dubbo中Directory解析
Directory代表多个Invoker,可以把它看成List Directory接口 Directory接口继承了Node接口: 1234567 public interface Directory ...
- nsq理解
核心概念 在讨论NSQ如何在实践中使用前,先理解NSQ队列的架构原理是非常值得的.它的设计很简单,可以通过几个核心概念来理解. Topic --一个topic就是程序发布消息的一个逻辑键,当程序第一次 ...
- BZOJ_2435_[Noi2011]道路修建_dfs
BZOJ_2435_[Noi2011]道路修建_dfs 题意: http://www.lydsy.com/JudgeOnline/problem.php?id=2435 分析: dfs搞定. 我怕爆栈 ...
- Treap与fhq_Treap学习笔记
1.普通Treap 通过左右旋来维护堆的性质 左右旋是不改变中序遍历的 #include<algorithm> #include<iostream> #include<c ...
- mysql5.7安装和修改密码
mysql5.7安装 第一 下载 https://downloads.mysql.com/archives/community/ 首先下载mysql5.7.18zip安装包 根据电脑配置选择32/64 ...
- 经典Hash函数的实现
Hash函数是指把一个大范围映射到一个小范围.把大范围映射到一个小范围的目的往往是为了节省空间,使得数据容易保存. 除此以外,Hash函数往往应用于查找上.所以,在考虑使用Hash函数之前,需要明白它 ...
- 新手篇丨Python任意网段Web端口信息探测工具
你学习Python的目的是什么?是想写爬虫爬取数据(数据.图片等内容),还是想自写自动化的小工具,又或是作为一个新手小白单纯的欣赏这门语言呢? 今天i春秋分享的是一篇关于多线程工具的文章,工具使用效率 ...
- 从壹开始微服务 [ DDD ] 之终篇 ║当事件溯源 遇上 粉丝活动
回首 哈喽~大家好,时间过的真快,关于DDD领域驱动设计的讲解基本就差不多了,本来想着周四再开一篇,感觉没有太多的内容了,剩下的一个就是验证的问题,就和之前的JWT很类似,就不打开一个章节了,而且这个 ...
- WinForm加载外部类库项目的集成开发模式
在项目开发中有一定的团队用到了Nuget.Coding:但是这用起来还是不太方方便,在Winform中呢,我们可以把一个人的项目当作一个类库项目,因为它生成的是一个dll文件,也就是单一文件,拥有了它 ...
- Windows下安装tesserocr
很难受,由于这两天重装了系统,又得重新配置环境了,而我在安装tesserocr的时候踩了一些坑,于是想写出来分享一下. 一.安装tesseract 要安装tesserocr,首先要下载tesserac ...