• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

vue 搞代码 4年前 (2022-01-08) 37次浏览 已收录 0个评论

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

vue页面设置

 //文件显示列表 点击上传<div class="el-upload__tip">只能上传jar文件,且不超过500kb</div><!-- :headers="head"--><!--:on-preview="handlePreview"--> /*文件上传前,判断文件名是否存在,等其他处理*/ 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; }); }, /*文件上传,重写文件上传方法,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一个对象的内容

 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: '',   <i class="el-icon-plus avatar-uploader-icon"></i>
 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.siz<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码网$</i>e / 1024 / 1024 <2; if (!isJPG) { this.$message.error('上传头像图片只能是 JPG 格式!'); } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!'); } return isJPG && isLt2M; }, 

css代码

 /* 图片上传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实现文件上传教程的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:vue+springboot+element+vue-resource实现文件上传教程

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址