LayUi多文件上传后端Java
前端代码
< div class = " layui-form-item" >
< label class = " layui-form-label" > 附件 label>
< div class = " layui-input-block layui-upload" >
< div class = " layui-upload" >
< button type = " button" class = " layui-btn layui-btn-normal" id = " AAAA" > 选择多文件 button>
< div class = " layui-upload-list" >
< table class = " layui-table" >
< thead>
< th> 文件名 th>
< th> 大小 th>
< th> 状态 th>
< th> 操作 th>
thead>
< tbody id = " demoListss" > tbody>
table>
div>
< button type = " button" class = " layui-btn" id = " testListAction" > 开始上传 button>
div>
div>
< input type = " hidden" name = " info" id = " info" >
div>
JavaScript代码
var index = 2 ;
var uploadNa= "" ;
var urls= "null" ;
layui. use ( [ 'form' , 'table' , 'laydate' , 'upload' ] , function ( ) {
var form = layui. form, table = layui. table;
var layer = layui. layer, upload = layui. upload;
var url = "" ;
var demoListView = $ ( '#demoListss' )
, uploadListIns = upload. render ( {
elem: '#AAAA'
, url: '${ctx}/w/uploadFileasdasdas'
, accept: 'file'
, multiple: true
, auto: false
, bindAction: '#testListAction'
, choose: function ( obj) {
var files = this . files = obj. pushFile ( ) ;
obj. preview ( function ( index, file, result) {
var tr = $ ( [ '+ index + '">'
, ' '+ file. name + ' '
, ' '+ ( file. size/ 1014 ) . toFixed ( 1 ) + 'kb '
, ' 等待上传 '
, ' '
, '重传 '
, '删除 '
, ' '
, ' ' ] . 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 = '' ;
} ) ;
demoListView. append ( tr) ;
} ) ;
}
, done: function ( res, index, upload) {
if ( res. code == 0 ) {
uploadNa= uploadNa+ res. msg+ "," ;
$ ( "#uploadname" ) . val ( uploadNa) ;
var tr = demoListView. find ( 'tr#upload-' + index)
, tds = tr. children ( ) ;
tds. eq ( 2 ) . html ( '上传成功 ' ) ;
tds. eq ( 3 ) . html ( '' ) ;
return delete this . files[ index] ;
}
this . error ( index, upload) ;
}
, error: function ( index, upload) {
var tr = demoListView. find ( 'tr#upload-' + index)
, tds = tr. children ( ) ;
tds. eq ( 2 ) . html ( '上传失败 ' ) ;
tds. eq ( 3 ) . find ( '.demo-reload' ) . removeClass ( 'layui-hide' ) ;
}
} ) ;
} )
Java代码
* /
@RequestMapping ( "/uploadFileasdasdas" )
@ResponseBody
public WebPageVo uploadFile ( @RequestParam ( "file" ) MultipartFile file) {
WebPageVo webPageVo = new WebPageVo ( ) ;
String fileName = null;
String fileNowName = null;
Boolean flag= false ;
Map< String, Object> param= new HashMap < > ( ) ;
if ( file. isEmpty ( ) ) {
webPageVo. setMsg ( "文件不能为空" ) ;
webPageVo. setCode ( 1 ) ;
return webPageVo;
}
try {
String path1 = Thread. currentThread ( ) . getContextClassLoader ( ) . getResource ( "" ) . getPath ( ) ;
path1= path1+ "upload/" ;
Date date = new Date ( ) ;
String paths= path1+ new SimpleDateFormat ( "yyyyMMdd" ) . format ( date) ;
File f = new File ( paths) ;
if ( ! f. exists ( ) ) {
f. mkdirs ( ) ;
fileName = file. getOriginalFilename ( ) ;
fileNowName = UUIDUtil. getUUID2 ( ) + "." + FilenameUtils. getExtension ( fileName) ;
File dest = new File ( paths+ "/" + fileNowName) ;
file. transferTo ( dest) ;
} else {
fileName = file. getOriginalFilename ( ) ;
fileNowName = UUIDUtil. getUUID2 ( ) + "." + FilenameUtils. getExtension ( fileName) ;
File dest = new File ( paths+ "/" + fileNowName) ;
file. transferTo ( dest) ;
}
webPageVo. setMsg ( fileNowName) ;
webPageVo. setCode ( 0 ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
webPageVo. setMsg ( "上传失败,请重新上传" ) ;
webPageVo. setCode ( 1 ) ;
}
return webPageVo;
}
工具类
import java. util. UUID;
public class UUIDUtil {
public static String getUUID ( ) {
return UUID. randomUUID ( ) . toString ( ) ;
}
public static String getUUID2 ( ) {
return UUID. randomUUID ( ) . toString ( ) . replace ( "-" , "" ) ;
}
}
返回数据类
import com. crux. posms. vo. w. DictVo;
import com. crux. posms. vo. w. RoleFuncVo;
import org. springframework. stereotype. Component;
import java. util. ArrayList;
import java. util. Collection;
import java. util. List;
@Component
public class WebPageVo {
private int code;
private String msg;
private long count;
private Collection< ? > data;
private String name;
private List< RoleFuncVo> roleFuncVosList;
public String getMsg ( ) {
return msg;
}
public void setMsg ( String msg) {
this . msg = msg;
}
public int getCode ( ) {
return code;
}
public void setCode ( int code) {
this . code = code;
}
public long getCount ( ) {
return count;
}
public void setCount ( long count) {
this . count = count;
}
public Collection< ? > getData ( ) {
return data;
}
public void setData ( Collection< ? > data) {
this . data = data;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public List< RoleFuncVo> getRoleFuncVosList ( ) {
return roleFuncVosList;
}
public void setRoleFuncVosList ( List< RoleFuncVo> roleFuncVosList) {
this . roleFuncVosList = roleFuncVosList;
}
}