栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

vue页面设置

      //文件显示列表
点击上传
只能上传jar文件,且不超过500kb
      

   
   beforeUpload(file){
    console.log("文件名",file.name,this.fileList)
    for (let i = 0; i  {
     this.$http.get('/aaaa/task/del?taskId='+this.taskId+'&name='+file.name).then(function(res) {
      ......
     });
    }).catch(() => {
     this.getJarList();
     return false;
    });
   },
   
   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一个对象的内容

name:"xxxx.jar"
status:"success"
uid:123456456

参数

this.param={
  taskId:this.taskId
}

springboot设置

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

  @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== 就是传参的属性(关键)。

imageUrl: '',


  
   
   
  

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代码


 .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实现文件上传教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/68240.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号