vue el-upload上传文件的示例代码
作者:我自是年少 时间:2024-05-11 09:13:47
话不多说 直接上代码
<el-upload
:action="actionUrl"
class="avatar-uploader"
:multiple="false"
name="files"
ref="upload"
:file-list="fileList"
:on-preview="handlePreview"
:on-success="handleSuccess"
:before-upload ="beforeUpload"
:http-request="httpRequest"
:on-exceed="handleExceed"
:on-change="handleChanged"
accept=".csv,.xls,.xlsx"
:auto-upload="false"
>
<el-button slot="trigger" size="small">选取文件</el-button>
<el-button style="margin-left: 10px;" size="small" type="primary" @click="submitUpload">上传到服务器</el-button>
<div slot="tip" class="el-upload__tip">只能上传csv/xslx/xsl文件,且不超过1M</div>
</el-upload>
actionUrl: `${env.imgCaptchaUrl}**/upload`,
fileList: [],
handleChanged(file,fileList){
this.fileList = fileList
},
handleExceed (file, fileList) {
console.log(file);
},
handleSuccess (res, file) {
console.log(file);
console.log(res);
},
handlePreview(file){
console.log(file);
},
beforeUpload (file) {
if (file.size / 1024 / 1024 > 1) {
Vue.$vux.toast.text('上传文件不超过1M')
return false
}
var ext = file.name.substring(file.name.lastIndexOf('.') + 1)
const extension =
ext === 'csv' ||
ext === 'CSV' ||
ext === 'xlsx' ||
ext === 'xls'
if (!extension) {
Vue.$vux.toast.text('上传文件格式只能为csv、xlsx/xls')
return false
}
},
httpRequest (opt) {
const _this = this
const file = opt.file
Vue.$vux.toast.text('文件上传中...')
var reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = function (e) {
let imgType = ''
var ext = file.name.substring(file.name.lastIndexOf('.') + 1)
if (ext === 'csv' ) {
imgType = 'csv'
}
if (ext === 'xlsx' || ext === 'xls') {
imgType = 'xlsx'
}
uploadCsv({
files: this.result.replace(`data:image/${imgType};base64,`, '')
})
.then(res => {
if (res.errno === 0) {
Vue.$vux.toast.text('上传成功')
_this.account.license_url = res.data.url
}
})
.catch(err => {})
}
},
submitUpload(){
if(this.fileList.length==0){
this.successDialog = "请先选择文件";
this.sussAlog = true;
return
}
this.$refs.upload.submit();
},
onDownload(){
let start = ""
let end = ""
if(this.form.time){
start = parseTime(this.form.time[0]);
end = parseTime(this.form.time[1]);
}
delete(this.form.time)
Object.assign(this.form, {
first_time: start,
end_time: end ,
});
let {
first_time,
end_time,
} = this.form;
window.open(this.downUrl+"lm/downloadModel?"+
"&first_time="+first_time+
"&end_time="+end_time
, '_blank');
},
来源:https://www.cnblogs.com/yangsg/p/13182228.html
标签:vue,上传文件,el-upload
0
投稿
猜你喜欢
用SELECT... INTO OUTFILE语句导出MySQL数据的教程
2024-01-13 19:50:52
python 将列表中的字符串连接成一个长路径的方法
2023-04-18 14:06:25
python实现sm2和sm4国密(国家商用密码)算法的示例
2021-11-17 08:02:13
Internet Explorer 8 Beta2 功能预览
2008-07-29 13:20:00
python初学者,用python实现基本的学生管理系统(python3)代码实例
2023-07-01 11:40:01
基于python实现数组格式参数加密计算
2021-08-20 23:29:54
解决Django layui {{}}冲突的问题
2023-07-23 15:22:18
Sql Server 字符串聚合函数
2024-01-17 08:15:34
linux下编译boost.python简单方法
2021-01-22 15:32:44
python列表中remove()函数的使用方法详解
2021-05-13 22:52:46
Python爬虫实战之用selenium爬取某旅游网站
2021-03-25 10:28:36
python能做哪些生活有趣的事情
2023-05-07 01:50:43
js DNA动态序列比对代码
2024-04-16 10:41:26
一次Mysql使用IN大数据量的优化记录
2024-01-29 07:49:19
如何对Python编译PyInstaller打包生成的exe文件进行反编译生成pyc、py源代码文件
2023-11-06 00:10:33
用javascript获得地址参数的两种方法
2008-04-30 12:17:00
Mysqlslap MySQL压力测试工具 简单教程
2024-01-15 20:10:10
CSS sprites图片拼合生成器
2007-10-15 12:25:00
PHP执行linux系统命令的常用函数使用说明
2023-10-19 11:28:39
python如何获取apk的packagename和activity
2023-07-11 20:50:19