• thymeleaf,bootstrap-fileinput 多文件上传


    组件遍历上传

    一、前端

    1. html>
    2. <html lang="zh" xmlns:th="http://www.thymeleaf.org" >
    3. <head>
    4. <th:block th:include="include :: header('修改固定资产信息')" />
    5. <th:block th:include="include :: datetimepicker-css" />
    6. <th:block th:include="include :: bootstrap-fileinput-css"/>
    7. head>
    8. <body class="white-bg">
    9. <div class="wrapper wrapper-content animated fadeInRight ibox-content">
    10. <form class="form-horizontal m" id="form-gdzcxx-edit" th:object="${bizGdzcxx}">
    11. <input name="gdzcxxId" th:field="*{gdzcxxId}" type="hidden">
    12. <div class="form-group">
    13. <label class="col-sm-3 control-label is-required">资产名称:label>
    14. <div class="col-sm-8">
    15. <input name="zcmc" th:field="*{zcmc}" class="form-control" type="text" required>
    16. div>
    17. div>
    18. <div class="form-group">
    19. <label class="col-sm-3 control-label">资产型号:label>
    20. <div class="col-sm-8">
    21. <input name="zcxh" th:field="*{zcxh}" class="form-control" type="text">
    22. div>
    23. div>
    24. <div class="form-group">
    25. <label class="col-sm-3 control-label">资产照片:label>
    26. <div class="col-sm-8">
    27. <input type="text" name="zczp" th:field="*{zczp}">
    28. <div class="file-loading">
    29. <input class="form-control file-upload" id="zczp" name="file" type="file" multiple>
    30. div>
    31. div>
    32. div>
    33. form>
    34. div>
    35. <th:block th:include="include :: footer" />
    36. <th:block th:include="include :: datetimepicker-js" />
    37. <th:block th:include="include :: bootstrap-fileinput-js"/>
    38. <script th:inline="javascript">
    39. var prefix = ctx + "biz/gdzcxx";
    40. $("#form-gdzcxx-edit").validate({
    41. focusCleanup: true
    42. });
    43. function submitHandler() {
    44. if ($.validate.form()) {
    45. $.operate.save(prefix + "/edit", $('#form-gdzcxx-edit').serialize());
    46. }
    47. }
    48. $(".file-upload").each(function (i) {
    49. var val = $("input[name='" + this.id + "']").val();
    50. var preView = val.split(',');
    51. $(this).fileinput({
    52. 'uploadUrl': ctx + 'common/upload',
    53. initialPreviewAsData: true,
    54. initialPreview: preView,
    55. maxFileCount: 10,
    56. maxFileSize: 1000,
    57. autoReplace: true
    58. }).on('fileuploaded', function (event, data, previewId, index) {
    59. //多文件上传后,将文件名路径用“,”拼接起来
    60. if(data.response.fileName){
    61. var filenames = data.response.fileName;
    62. var currentFilename = $("input[name='" + event.currentTarget.id + "']").val();
    63. if(currentFilename!=''){
    64. filenames = currentFilename +',' +filenames;
    65. }
    66. $("input[name='" + event.currentTarget.id + "']").val(filenames);
    67. }
    68. }).on('fileremoved', function (event, id, index) {
    69. var fileIndex = index.replace('init-','');
    70. var currentFilename = $("input[name='" + event.currentTarget.id + "']").val();
    71. var fileNames = currentFilename.split(',');
    72. var lastFileNames = '';
    73. for(var i in fileNames){
    74. if(i == parseInt(fileIndex)){
    75. continue;
    76. }
    77. lastFileNames += fileNames[i]+',';
    78. }
    79. lastFileNames = lastFileNames.split(',').slice(0,-1).join(',');
    80. $("input[name='" + event.currentTarget.id + "']").val(lastFileNames);
    81. })
    82. $(this).fileinput('_initFileActions');
    83. });
    84. script>
    85. body>
    86. html>

    二、后端

    1. @Controller
    2. @RequestMapping("/common")
    3. public class CommonController
    4. {
    5. private static final Logger log = LoggerFactory.getLogger(CommonController.class);
    6. @Autowired
    7. private ServerConfig serverConfig;
    8. /**
    9. * 通用上传请求(单个)
    10. */
    11. @PostMapping("/upload")
    12. @ResponseBody
    13. public AjaxResult uploadFile(MultipartFile file) throws Exception
    14. {
    15. try
    16. {
    17. // 上传文件路径
    18. String filePath = "D:/upload";
    19. // 上传并返回新文件名称
    20. String fileName = FileUploadUtils.upload(filePath, file);
    21. String url = serverConfig.getUrl() + fileName;
    22. AjaxResult ajax = AjaxResult.success();
    23. ajax.put("url", url);
    24. ajax.put("fileName", fileName);
    25. ajax.put("newFileName", FileUtils.getName(fileName));
    26. ajax.put("originalFilename", file.getOriginalFilename());
    27. return ajax;
    28. }
    29. catch (Exception e)
    30. {
    31. return AjaxResult.error(e.getMessage());
    32. }
    33. }
    34. }
    1. @Component
    2. public class ServerConfig
    3. {
    4. /**
    5. * 获取完整的请求路径,包括:域名,端口,上下文访问路径
    6. *
    7. * @return 服务地址
    8. */
    9. public String getUrl()
    10. {
    11. HttpServletRequest request = ServletUtils.getRequest();
    12. return getDomain(request);
    13. }
    14. public static String getDomain(HttpServletRequest request)
    15. {
    16. StringBuffer url = request.getRequestURL();
    17. String contextPath = request.getServletContext().getContextPath();
    18. return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
    19. }
    20. }

  • 相关阅读:
    kantts docker化
    在Markdown文件中快速插入本地图片
    创建对象在堆区如何分配内存
    数据推荐 | 自然场景OCR文字识别数据集一览
    Vue.js vs React vs Angular
    带团队后的日常思考(十四)
    面试题:Java中为什么只有值传递?
    JMeter入门教程(16)——非GUI运行
    Maven高级
    Java系列 - 反射
  • 原文地址:https://blog.csdn.net/yetyrain/article/details/133829835