前端采用layui框架,讲解多文件上传的完整实现功能。
前端html重点代码如下:
java" id="highlighter_207918">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<div class = "layui-form-item" >
<label class = "layui-form-label" >上传文件</label>
<div class = "layui-input-block" >
<div class = "layui-upload" >
<button type= "button" class = "layui-btn layui-btn-normal" id= "testlist" >选择多文件</button>
<div class = "layui-upload-list" >
<table class = "layui-table" >
<thead>
<tr><th>文件名</th>
<th>大小</th>
<th>状态</th>
<th>操作</th>
</tr></thead>
<tbody id= "demolist" ></tbody>
</table>
</div>
<button type= "button" class = "layui-btn" id= "testlistaction" >开始上传</button>
</div>
</div>
</div>
|
相应的,js代码如下所示:
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
54
55
56
57
58
|
layui.use( 'upload' , function(){
var $ = layui.jquery,upload = layui.upload;
//多文件列表示例
var demolistview = $( '#demolist' )
,uploadlistins = upload.render({
elem: '#testlist'
,url: '/upload'
,accept: 'file'
,data:{} //可放扩展数据 key-value
,multiple: true
,auto: false
,bindaction: '#testlistaction'
,choose: function(obj){
var files = this .files = obj.pushfile(); //将每次选择的文件追加到文件队列
//读取本地文件
obj.preview(function(index, file, result){
var tr = $([ '<tr id="upload-' + index + '">'
, '<td>' + file.name + '</td>'
, '<td>' + (file.size/ 1014 ).tofixed( 1 ) + 'kb</td>'
, '<td>等待上传</td>'
, '<td>'
, '<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
, '<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">删除</button>'
, '</td>'
, '</tr>' ].join( '' ));
//单个重传
tr.find( '.demo-reload' ).on( 'click' , function(){
obj.upload(index, file);
});
//删除
tr.find( '.demo-delete' ).on( 'click' , function(){
delete files[index]; //删除对应的文件
tr.remove();
uploadlistins.config.elem.next()[ 0 ].value = '' ; //清空 input file 值,以免删除后出现同名文件不可选
});
demolistview.append(tr);
});
}
,done: function(res, index, upload){
if (res.code == 0 ) //上传成功
var tr = demolistview.find( 'tr#upload-' + index)
,tds = tr.children();
tds.eq( 2 ).html( '<span style="color: #5fb878;">上传成功</span>' );
tds.eq( 3 ).html( '' ); //清空操作
return delete this .files[index]; //删除文件队列已经上传成功的文件
} //code为后台传回来的数据,具体多少自己定,
//后台只能传回json格式数据,不然会走error函数;
,error: function(index, upload){
}
})
});
|
以上即是前端功能的实现,后端方面,在service层impl下创建文件上传的函数:
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
|
public string uploadnoticefile(multipartfile filelist) {
try {
string pathname = filepath;
string timemillis = long .tostring(system.currenttimemillis()); //时间戳
string filename = timemillis + filelist.getoriginalfilename();
file dir = new file(pathname);
if (!dir.exists()) {
dir.mkdirs();
}
string filepath = pathname + filename;
file serverfile = new file(filepath);
filelist.transferto(serverfile);
//存入数据库
noticefile noticefile = new noticefile();
noticefile.setnofilename(filename);
noticefile.setnofilepath(filepath);
noticefile.setnoid(0l);
noticefilerepository.save(noticefile);
return "1" ;
} catch (exception e) {
e.printstacktrace();
return "0" ;
}
}
|
noticefile是我个人在写项目时创建的类,读者可根据实际情况自行运用。
然后,在controller层中创建相应的函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@requestmapping (value = "/upload" , method = requestmethod.post)
@responsebody
public map<string, object> noticefile( @requestparam (name = "file" ) multipartfile files) {
string msg = noticefileservice.uploadnoticefile(files);
map map = new hashmap();
if (msg == "1" ) {
map.put( "code" , "0" );
} else {
map.put( "code" , "1" );
}
return map;
}
|
以上,即实现了多文件上传的全部功能。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_42259631/article/details/80999415