elementUI 和vue 还有axios +java的完整示例, 代码敲了很久, 累死了, 以后用就直接复制了 ,很值吧!!!

1.html

<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>导入</title>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="myUpload">
<el-button type="primary" size="mini" @click="uploadFile">导入</el-button>
<!--上传-->
<el-dialog title="上传" width="40%" :visible.sync="uploadTemplateDialog">
<el-row>
<el-col :span="22">
<el-upload class="upload-demo"
ref="upload"
action=""
:accept="acceptFileType"
:limit="1"
:on-exceed="handleExceed"
:before-upload="beforeUpload"
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
:auto-upload="false">
<el-button slot="trigger" size="small" type="primary">选取Excel格式文件</el-button>
<div slot="tip" class="el-upload_tip">只能上传.xls文件,且不超过1M</div>
</el-upload> </el-col>
</el-row>
<span slot="footer" class="dialog-footer">
<el-button @click="submitUpload" type="primary" size="mini" :loading="uploadLoading" > 确定上传</el-button>
<el-button @click="uploadTemplateDialog=false" size="mini">取消</el-button>
</span> </el-dialog> </div> <script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/element-ui@2.6.1/lib/index.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script >
new Vue({
el:'#myUpload',
data:function(){
return{
uploadTemplateDialog:false,
fileList:[],
uploadLoading:false,
acceptFileType:'.xls',
downLoadLoading:'',
}
},
//钩子函数,页面加载执行
created:function(){ },
//钩子函数,页面加载完成后执行
mounted(){ },
//函数方法
methods:{
uploadFile(){
this.uploadLoading=false;
var that=this;
this.fileList=[];
this.uploadTemplateDialog=true;
setTimeout(function(){
that.$refs.upload.clearFiles();
},100);
},
handleExceed(files,fileList){
this.$message.warning('只能选择1个文件!');
},
submitUpload(){
this.uploadLoading=true;
var that=this;
setTimeout(function () {
if(that.$refs.upload.$children[0].fileList.length==1){
that.$refs.upload.submit();
}else{
that.uploadLoading=false;
that.$message({
type:'error',
showClose:true,
duration:3000,
message:'请选择文件!'
});
};
},100);
},
handleRemove(file,fileList){
//console.log(file,fileList);
},
handlePreview(file){
//console.log(file);
},
beforeUpload(file){
var that=this;
//文件类型
var fileName=file.name.substring(file.name.lastIndexOf('.')+1);
if(fileName!='xls'){
that.uploadTemplateDialog=false;
that.$message({
type:'error',
showClose:true,
duration:3000,
message:'文件类型不是.xls文件!'
});
return false;
}
//读取文件大小
var fileSize=file.size;
console.log(fileSize);
if(fileSize>1048576){
that.uploadTemplateDialog=false;
that.$message({
type:'error',
showClose:true,
duration:3000,
message:'文件大于1M!'
});
return false;
}
that.downloadLoading=that.$loading({
lock:true,
text:'数据导入中...',
spinner:'el-icon-loading',
background:'rgba(0,0,0,0.7)'
});
let fd=new FormData();
fd.append('file',file);
fd.append('_t1',new Date());
axios({
method:'post',
url:'/upload/'+new Date().getTime(),
data:fd,
headers:{"Content-Type":"multipart/form-data;boundary="+new Date().getTime()}
}).then(rsp=>{
that.downloadLoading.close();
that.uploadLoading=false;
let resp=rsp.data
if(resp.resultCode==200){
that.uploadTemplateDialog=false;
that.$message.success(resp.resultMsg);
//that.queryData();//更新数据
}else{
that.uploadTemplateDialog=false;
that.$message({
type:'error',
showClose:true,
duration:60000,
message:resp.resultMsg
});
}
}).catch(error=> {
that.downloadLoading.close();
that.uploadLoading=false;
that.uploadTemplateDialog=false;
that.$message({
type:'error',
showClose:true,
duration:60000,
message:'请求失败! error:'+error
});
})
return false;
}
}
})
</script>
</body>
</html>

2.java

@RequestMapping(value="/upload/{time}",method={RequestMethod.POST})
@ResponseBody
public HttpResult upload(@PathVariable("time") String time,@RequestParam("file") MultipartFile file,HttpServlet request){
String fileName=file.getOriginalFilename();
String prefix=fileName.substring(filename.lastIndexOf(".")+1); // 后缀: "xls"
String path=request.getServletContext().getRealPath(File.separator+"myFile");
String date=String.valueOf(new Date().getTime());
if(!new File(path).exists()){
new File(path).mkdir();
}
File file2=new File(path+File.separator+date+"_"+fileName);
file.transferTo(files);
return HttpResult.ok();
}

3.效果图

效果图还是不错的吧, 后台java 我就截取了一段代码, 大家可以自己发挥, 前端基本都完整了, 可以完全套用, 大家给个意见吧!!!

elementUI vue upload完整示例的更多相关文章

  1. element-ui(vue)upload组件的http-request方法的使用

    element-ui(vue)upload组件的http-request方法的使用 官方文档: http-request方法有一个默认的参数 content content 是一个object对象:里 ...

  2. elementUI vue tree input 懒加载 输入下拉树型示例 点击其他区域关闭自定义div

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  3. 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)

    [原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...

  4. asp.net mvc5 使用百度ueditor 本编辑器完整示例(下)配置上传播放视频

    通过 asp.net mvc5 使用百度ueditor 本编辑器完整示例(上)介绍,可以上传图片到服务器了,也可以上传小的视频文件,并且由百度编辑器自动加入html5<video>标签播放 ...

  5. 【第四篇】ASP.NET MVC快速入门之完整示例(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  6. WCF服务开发与调用的完整示例

    WCF服务开发与调用的完整示例 开发工具:VS2008 开发语言:C# 开发内容:简单的权限管理系统 第一步.建立WCF服务库 点击确定,将建立一个WCF 服务库示例程序,自动生成一个包括IServi ...

  7. springmvc 项目完整示例06 日志–log4j 参数详细解析 log4j如何配置

    Log4j由三个重要的组件构成: 日志信息的优先级 日志信息的输出目的地 日志信息的输出格式 日志信息的优先级从高到低有ERROR.WARN. INFO.DEBUG,分别用来指定这条日志信息的重要程度 ...

  8. C连接MySQL数据库开发之Linux环境完整示例演示(增、删、改、查)

    一.开发环境 ReadHat6.3 32位.mysql5.6.15.gcc4.4.6 二.编译 gcc -I/usr/include/mysql -L/usr/lib -lmysqlclient ma ...

  9. Struts 2.3.4.1完整示例

    [系统环境]Windows 7 Ultimate 64 Bit [开发环境]JDK1.6.21,Tomcat6.0.35,MyEclipse10 [其他环境]Struts2.3.4.1 [项目描述]S ...

随机推荐

  1. 《http权威指南》读书笔记18

    概述 最近对http很感兴趣,于是开始看<http权威指南>.别人都说这本书有点老了,而且内容太多.我个人觉得这本书写的太好了,非常长知识,让你知道关于http的很多概念,不仅告诉你怎么做 ...

  2. CSS揭秘—多重边框(二)

    前言: 所有实例均来自<CSS揭秘>,该书以平时遇到的疑难杂症为引,提供解决方法,只能说秒极了,再一次刷新了我对CSS的认知 该书只提供了关键CSS代码,虽然有在线示例代码链接,但访问速度 ...

  3. Cloud-Platform部署学习

    1. Cloud-Platform部署学习 1.1. 介绍 Cloud-Platform是国内首个基于Spring Cloud微服务化开发平台,核心技术采用Spring Boot2以及Spring C ...

  4. dubbo实用知识点总结(三)

    1. 服务降级 2. 优雅停机 3. 主机绑定 4. 访问日志 5. Multicast注册中心 6. zookeeper注册中心 7. 推荐用法 8. 容量规划 9. 基准测试工具包

  5. compose函数

    compose函数 在学习redux源码的时候看到了其中的工具函数compose,compose函数的作用就是组合函数,依次组合传入的函数: 后一个函数作为前一个函数的参数 最后一个函数可以接受多个参 ...

  6. new 操作符 做了什么

    new 操作符 做了什么 new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例. 假设Test是一个构造函数,通常在创建对象的实例时,要使用new,eg:test = new ...

  7. Java虚拟机知识汇总

    先膜一波布丁大佬hhh

  8. chrome强制刷新,非ctrl+f5

    开发时,经常有ctrl+f5无法做到真正的强制刷新,以下可以帮到你 Ctrl+Shift+Del 清除Google浏览器缓存的快捷键 Ctrl+Shift+R 重新加载当前网页而不使用缓存内容

  9. 设置build.gradle打包时自动加时间

    在build.gradle中添加以下函数: def releaseTime() { return new Date().format("yyyyMMddHHmm", TimeZon ...

  10. 课程回顾-Convolutional Neural Networks

    为什么卷积层计算量更低paddingStrided convolution多维卷积LeNet 参数卷积网络的好处参数共享稀疏连接经典网络实现LeNet-5AlexNetVGGResNet残差块iden ...