vue+springboot+element+vue-resource实现文件上传
vue+springboot+element+vue-resource实现文件上传
vue页面设置
<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一个对象的内容
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) {
//此时没有选择文件
}
....
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示