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 ...
随机推荐
- key.go
package].Key) if err = waitDelete(ctx, client, lastKey, resp.Header.Revision); err != nil { ...
- bzoj 2829 计算几何
将每张卡四个角的圆心跑graham出正常凸包,再加上一个圆就好了. 要注意先输入的是x,找点时三角函数瞎换就过了.. #include<cstdio> #include<cstrin ...
- bzoj 1592 dp
就是dp啊 f[i][j]表示到第i位,最后一位高度是j的最小花费 转移::f[i][j]=minn(f[i-1][k])+abs(a[i]-num[j]);(k<=j) #include< ...
- Sql语言简介——检索数据
检索数据可以通过SELECT语句来实现. select子句:用于选择数据表.视图中的列. into子句:用于将原表中的结构和数据插入新表中. from子句:用于指定数据来源,包括表.视图和其他sele ...
- Netty自定义协议解析原理与应用
目前,大家都选择Netty做为游戏服务器框架网络通信的框架,而且目前也有很多优秀的产品是基于Netty开发的.它的稳定性,易用性和高效率性已得到广泛的认同.在游戏服务器开发中,选择netty一般就意味 ...
- CSS3实例分享之多重背景的实现(Multiple backgrounds)
CSS3的诞生为我们解决了这一问题,在CSS3里,通过background-image或者background可以为一个容器设置多张背景图像,也就是说可以把不同背景图象只放到一个块元素里. 首先我们来 ...
- shell脚本添加实例化参数
通过shell脚本给GMP系统添加一个环境变量参数dateSwitchTimeInterval 1. insert.sh #!/bin/sh . ~/apphome/aic_export.sh #连接 ...
- SQL Android
SQLite是一款轻量级的关系型数据库,它的运算速度非常快,占用资源很少. 一般有以下几个关键步骤: 1.创建数据库 2.创建表 3.操作:增删改查 4.关闭数据库 5.删除表(非必选) SQLite ...
- 浅谈URL跳转与Webview安全
学习信息安全技术的过程中,用开阔的眼光看待安全问题会得到不同的结论. 在一次测试中我用Burpsuite搜索了关键词url找到了某处url,测试一下发现waf拦截了指向外域的请求,于是开始尝试绕过.第 ...
- 领域驱动设计学习之路—DDD的原则与实践
本文是我学习Scott Millett & Nick Tune编著的<领域驱动设计模式.原理与实践>一书的学习笔记,一共会分为4个部分如下,此文为第1部分: ① 领域驱动设计的原则 ...