【bootstrap-fileinput】前后台完整示例

时间:2024-04-09 11:25:39

【bootstrap-fileinput】前后台完整示例

2018年03月01日 17:34:50

阅读数:2672

效果图:

【bootstrap-fileinput】前后台完整示例

HTML:

 

 
  1. <!-- 引入css文件 -->

  2. <link rel="stylesheet" type="text/css" href="css/fileinput.min.css"/>

  3. <!-- 引入js文件 -->

  4. <script type="text/javascript" src="js/fileinput.min.js"></script>

  5. <script type="text/javascript" src="js/zh.js"></script>

  6.  
  7. <div class="modal-body">

  8. <a href="" class="form-control" style="border:none;">下载模板</a>

  9. <input type="file" name="reportFile" id="reportFile" multiple class="file-loading" />

  10. </div>

JS:

 
  1. //初始化fileinput

  2. var oFileInput = new FileInput();

  3. //参数1:控件id、参数2:上传地址

  4. oFileInput.Init("reportFile", "/report/uploadReport");

 
  1. //初始化fileinput

  2. var FileInput = function () {

  3. var oFile = new Object();

  4.  
  5. //初始化fileinput控件(第一次初始化)

  6. oFile.Init = function(ctrlName, uploadUrl) {

  7.     var control = $('#' + ctrlName);

  8.  
  9. //初始化上传控件的样式

  10. control.fileinput({

  11. language: 'zh', //设置语言

  12. uploadUrl: uploadUrl, //上传的地址

  13. allowedFileExtensions: ['jpg', 'gif', 'png', 'pdf'], //接收的文件后缀

  14. showUpload: true, //是否显示上传按钮

  15. showCaption: false, //是否显示标题

  16. browseClass: "btn btn-primary", //按钮样式

  17. //dropZoneEnabled: false, //是否显示拖拽区域

  18. //minImageWidth: 50, //图片的最小宽度

  19. //minImageHeight: 50, //图片的最小高度

  20. //maxImageWidth: 1000, //图片的最大宽度

  21. //maxImageHeight: 1000, //图片的最大高度

  22. //maxFileSize: 0, //单位为kb,如果为0表示不限制文件大小

  23. //minFileCount: 0,

  24. maxFileCount: 10, //表示允许同时上传的最大文件个数

  25. enctype: 'multipart/form-data',

  26. validateInitialCount:true,

  27. previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",

  28. msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",

  29. uploadExtraData:function (previewId, index) { //传参

  30. var data = {

  31. "reportGroupId": $('#lbl_groupId').html(), //此处自定义传参

  32. };

  33. return data;

  34. }

  35. });

  36.  
  37. //导入文件上传完成之后的事件

  38. $("#reportFile").on("fileuploaded", function (event, data, previewId, index) {

  39. //报告table刷新

  40. showDataGrid_report($('#lbl_groupId').html());

  41. });

  42. }

  43. return oFile;

  44. };

JAVA:

 
  1. /**

  2.      * 报告上传

  3.      * */

  4.     @RequestMapping(value = "/uploadReport", method = RequestMethod.POST)

  5.     public ApiResponse uploadReport(HttpServletRequest request, HttpServletResponse response) {

  6.         Map<String, Object> result = new HashMap<String, Object>();

  7.         //获取参数

  8.         String reportGroupId = getParam(request, "reportGroupId");

  9.         

  10.         //调用通用接口上传文件

  11.         result = uploadFile(request, "reportFile", reportGroupId);

  12.         

  13.         ApiResponse ret = new ApiResponse();

  14.         ret.setMap(result);

  15.         return ret;

  16.     }

  17.  
  18. /**

  19. * 上传文件通用接口

  20. *

  21. * @param request 请求体

  22. * @param dstFileName html上传组件中(input中name属性),上传文件体名称,通过此名称获取所有上传的文件map

  23. * @param reportGroupId (特殊)上传报告所述报告组id

  24. * */

  25. protected Map<String, Object> uploadFile(HttpServletRequest request, String dstFileName, String reportGroupId) {

  26. Map<String, Object> ret = new HashMap<String, Object>();

  27.  
  28. //判断保存文件的路径是否存在

  29. File fileUploadPath = new File(FILE_UPLOAD_PATH);

  30. if (!fileUploadPath.exists()) {

  31. fileUploadPath.mkdir();

  32. }

  33.  
  34. if (ServletFileUpload.isMultipartContent(request)) {

  35. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

  36. List<MultipartFile> fileList = multipartRequest.getFiles(dstFileName);

  37. for (MultipartFile item : fileList) {

  38. String fileName = ""; //当前上传文件全名称

  39. String fileType = ""; //当前上传文件类型

  40. String saveFileName = ""; //保存到服务器目录的文件名称

  41. String reportAddr = ""; //保存到服务器目录的文件全路径

  42. try {

  43. fileName = item.getOriginalFilename();

  44. fileType = item.getContentType();

  45. saveFileName = getCurrentTime("yyyyMMddHHmmss") + "_" + fileName;

  46. reportAddr = fileUploadPath + "/" + saveFileName;

  47. reportAddr = reportAddr.replace("/", File.separator).replace("\\", File.separator);

  48.  
  49. File savedFile = new File(fileUploadPath, saveFileName);

  50. item.transferTo(savedFile);

  51.  
  52. //上传文件成功,保存文件信息到表reportDetail

  53. Map<String, Object> param = new HashMap<String, Object>();

  54. param.put("reportGroupId", reportGroupId);

  55. param.put("reportName", fileName);

  56. param.put("reportType", fileType);

  57. param.put("reportAddr", reportAddr);

  58. param.put("createTime", getCurrentTime("yyyy-MM-dd HH:mm:ss"));

  59. param.put("lastOper", "xxxxxxx");

  60. ApiResponse r = (ApiResponse) saveEntity("/report/saveReportDetail", param, ApiResponse.class);

  61.  
  62. if (r.getCode() == 0) {

  63. ret.put("success", true);

  64. } else {

  65. ret.put("success", false);

  66. ret.put("message", r.getMessage());

  67. }

  68. } catch (Exception e) {

  69. logger.error(e.getMessage());

  70. ret.put("success", false);

  71. ret.put("message", e.getMessage());

  72. }

  73. }

  74. }

  75. return ret;

  76. }

PS:后台接收文件时用了for循环,其实bootstrap-fileinput插件在多文件上传时,是采用多次调用'uploadUrl'实现文件上传,所以当前for循环只执行了一遍。然后返回结果给页面,页面对当前文件上传状态做处理。