前端采用layui框架,講解多文件上傳的完整實現功能。
前端html重點代碼如下:
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