页面:
<form enctype = "multipart/form-data" action="http://localhost:8080/PoliceSystem/excel/importpublic" method="post" id="importingFile">
<input type="file" name="FileUpload" id="FileUpload">
<button type="button" id="sc">上传</button>
<script>
$(function(){
$('#sc').click(function(){
var FileUpload = new FormData(document.getElementById('importingFile'));
$.ajax({
type:'post',
url:'http://localhost:8080/PoliceSystem/excel/importpublic',
data:FileUpload,
processData:false,
contentType:false,
success:function(message){
alert(message)
},
error:function(message){
alert(message)
}
})
})
})
</script>
</form>
后台:
@RequestMapping(value = "/importpublic",method= RequestMethod.POST)
@ResponseBody
public AjaxResult importpublic(@RequestParam(value = "FileUpload",required = false) MultipartFile FileUpload, HttpServletRequest request, HttpServletResponse response) throws Exception {
InputStream in = FileUpload.getInputStream();
// 读取excel中的内容
Map<String, Object> lineDownOrders = ExcelUtils.getBankListByExcelPublic(in);
System.out.println(lineDownOrders);
return success(AjaxResult.Type.SUCCESS,"导入成功",null);
}