bootstrap 文件上传fileinput

网友投稿 671 2022-09-09

bootstrap 文件上传fileinput

bootstrap 文件上传fileinput

页面中引入引入:fileinput.js

js初始化:

后台js进行初始化;

$("#customerexcel").fileinput({ language : 'zh', // 设置语言 uploadUrl : getProjectURL() + 'customer/drcustomer', // 上传的地址 allowedFileExtensions : [ 'xls' ],// 接收的文件后缀 showUpload : true, // 是否显示上传按钮 showCaption : false, maxFileCount : 1 // 是否显示标题 }).on('fileuploaded', function(event, data) { alert(data.response.msg); })

最终的实现结果:

因为我这个是导入的excel,后台:

@RequestMapping("/drcustomer") @ResponseBody public JSONObject drCustomer(HttpServletResponse response, HttpServletRequest request) { try { int count = 0; if (ServletFileUpload.isMultipartContent(request)) { MultipartHttpServletRequest fileUpload = (MultipartHttpServletRequest) request; MultipartFile file = fileUpload.getFile("file"); Workbook workbook = Workbook.getWorkbook(file.getInputStream()); Sheet sheet = workbook.getSheet(0); int rows = sheet.getRows(); for (int i = 1; i < rows; i++) { Cell[] cells = sheet.getRow(i); Customer customer = new Customer(); customer.setCode(cells[0].getContents()); // 判断是否存在相同的代码 if (customerService.isExistSameCode(customer.getCode())) { count++; continue; } customer.setCusname(cells[1].getContents()); customer.setComtaxno(cells[2].getContents()); customer.setAddresstel(cells[3].getContents()); customer.setSj(cells[4].getContents()); customer.setEmail(cells[5].getContents()); customer.setAccount(cells[6].getContents()); customer.setAddtime(new Date()); customerService.save(customer); } String msg=""; if(count>0) { msg="客户导入成功:有" + count + "在相同的客户代码,没有导入"; }else { msg="客户导入成功"; } return BuildJsonOfObject.getJsonString("1",msg); } else { return BuildJsonOfObject.getErrorJSONString("0", "文件上传失败"); } }catch(BiffException e) { throw new RuntimeException("内容解析失败,请将当前excel另存为Microsoft Excel 97-2003 文件的*.xls"); } catch (Exception e) { e.printStackTrace(); return BuildJsonOfObject.getErrorJSONString("0", e.getMessage()); } }

希望对有所帮助

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python 时间处理datetime实例(python培训)
下一篇:415 (Unsupported Media Type)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~