服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - vue+springboot+element+vue-resource实现文件上传教程

vue+springboot+element+vue-resource实现文件上传教程

2021-11-01 16:22紫月java JavaScript

这篇文章主要介绍了vue+springboot+element+vue-resource实现文件上传教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

vue页面设置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
   <el-upload
    class="upload-demo"
    action=""
    :before-upload="beforeUpload" //上传前操作
    :before-remove="beforeRemove" //移除钱操作
    :multiple="false"  //禁止多选
    :http-request="myUpload" //文件上传,重写文件上传方法,action的路径不会起作用
    accept=".jar"  //限制文件选择类型
    :drag="false"
    :data="param" //参数
    :file-list="fileList">//文件显示列表
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">只能上传jar文件,且不超过500kb</div><!-- :headers="head"-->
   </el-upload><!--:on-preview="handlePreview"-->
 
/*文件上传前,判断文件名是否存在,等其他处理*/
beforeUpload(file){
 console.log("文件名",file.name,this.fileList)
 for (let i = 0; i <this.fileList.length ; i++) {
  if (this.fileList[i].name==file.name) {
   this.$message.info("文件已存在");
   return false;
  }
 }
 this.file=file;
 
 return true;
},
 /*文件移除前,提示是否删除*/
beforeRemove(file,fileList){//delJar
 this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
  confirmButtonText: '确定',
  cancelButtonText: '取消',
  type: 'warning'
 }).then(() => {
  this.$http.get('/aaaa/task/del?taskId='+this.taskId+'&name='+file.name).then(function(res) {
   ......
  });
 }).catch(() => {
  this.getJarList();
  return false;
 });
},
/*文件上传,重写文件上传方法,action的路径不会起作用*/
myUpload(file){
 let fd = new FormData();
 fd.append('file',this.file);//传文件
 fd.append('taskId',this.taskId);//传其他参数
 // fd.append('filename',file.name);//传其他参数
 this.$http.post('/aaaa/task/add',fd).then(function(res) {
  ....
 });
},

fileList一个对象的内容

?
1
2
3
name:"xxxx.jar"
status:"success"
uid:123456456

参数

?
1
2
3
this.param={
  taskId:this.taskId
}

springboot设置

1.请求的注解:produces = "multipart/form-data;charset=utf-8", method = RequestMethod.POS

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RequestMapping(value = "/add", produces = "multipart/form-data;charset=utf-8", method = RequestMethod.POST)
public String addJar(int taskId, HttpServletRequest request) throws IOException, ServletException {
  ....
  //获取文件
  Part part = request.getPart("file");// input的name值
  String dis = part.getHeader("Content-Disposition");
  // 获取文件名--sdsf.jar
  String fna = dis.substring(dis.indexOf("filename=") + 10, dis.length() - 1);
  String fname = fna.substring(fna.lastIndexOf("\\") + 1, fna.length());// 有的浏览器获取的是路径+文件名
  // 若是文件名为空,说明此时没有选择文件,返回,文件上传失败,选择文件
  if (fname.length() < 1) {
   //此时没有选择文件
  }
  ....
}

补充知识:elementUI upload图片文件上传到指定后端接口解决方法

1. 一般后端提供接口上传文件都有参数。如果我们不传参就会报错或显示图片不存在,上传失败。所以我们要参考他的文档。action 是上传路径; ==name== 就是传参的属性(关键)。

vue+springboot+element+vue-resource实现文件上传教程

?
1
2
3
4
5
6
7
8
9
imageUrl: '',
 
<el-form-item label="封面图片" :required="true">
  <el-upload class="avatar-uploader" action="http://xxx.cn/xx/file/uploadImg/" name='photo' :show-file-list="false"
   :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
   <img v-if="imageUrl" :src="imageUrl" class="avatar">
   <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  </el-upload>
</el-form-item>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
handleAvatarSuccess(res, file) {
    console.log(res)
    console.log(file)
    this.imageUrl = URL.createObjectURL(file.raw);
    console.log(this.imageUrl)
   },
   //验证图片格式
   beforeAvatarUpload(file) {
    const isJPG = file.type === 'image/jpeg';
    const isLt2M = file.size / 1024 / 1024 < 2;
 
    if (!isJPG) {
     this.$message.error('上传头像图片只能是 JPG 格式!');
    }
    if (!isLt2M) {
     this.$message.error('上传头像图片大小不能超过 2MB!');
    }
    return isJPG && isLt2M;
   },

css代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* 图片上传css */
 .avatar-uploader /deep/.el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
 }
 
 .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
 }
 
 .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
 }
 .avatar {
  width: 100px;
  height: 100px;
  display: block;
 }

参考elementUI文档:https://element.eleme.cn/#/zh-CN/component/upload

以上这篇vue+springboot+element+vue-resource实现文件上传教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/ziyue7575/p/13704151.html

延伸 · 阅读

精彩推荐