文件上传组件IE9适配

[!TIP]
因为elementUI的文件上传组件不兼容IE9,本工程采用layUI的上传组件,layUI文件上传组件底层采用的是jquery的上传组件,兼容ie9

更多关于layUI文件上传组件可以参考:https://www.layui.com/demo/upload.html

前端页面

[!DANGER|label: 此案例建立在入门工程前端开发的基础上,请完成入门开发指南中的前端工程搭建再进行此案例。 前端工程搭建]

1.在入门开发指南中建立的todo-views工程中的resources目录下创建一个目录,目录名称templates/upload
uploadPath.png
2.在resources/templates/test目录下创建 file_upload.html 文件,并将下方内容复制进去

  1. <!DOCTYPE html>
  2. <meta charset="UTF-8">
  3. <html lang="en" xmlns:th="http://www.springframework.org/schema/mvc">
  4. <head>
  5. <!--公共模块引入-->
  6. <div th:include="../templates/include/sys/common::sys_core"></div>
  7. <!--layUI引入-->
  8. <link rel="stylesheet" th:href="@{/static/plugins/layui/css/layui.css}">
  9. <script th:src="@{/static/plugins/layui/layui.js}"></script>
  10. </head>
  11. <body>
  12. <div id="app">
  13. <button type="button" class="layui-btn" id="test"><i class="layui-icon"></i>上传文件</button>
  14. </div>
  15. <script>
  16. layui.use('upload', function() {
  17. var $ = layui.jquery, upload = layui.upload;
  18. upload.render({
  19. elem: '#test',
  20. // 服务器上传接口(请修改为自己的上传接口),示例中的baseUrl为views工程中定义的后端工程前缀全局变量
  21. url: baseUrl+ '/test/fileUpload?token='+getToken(),
  22. // 设置允许上传的文件类型
  23. accept: 'file',
  24. // 允许上传的文件后缀
  25. // exts:'txt',
  26. // 上传接口完成的回调
  27. done: function(res){
  28. if(res.code=="200") {
  29. vm.$message({
  30. message: '上传成功',
  31. type: 'success'
  32. });
  33. }
  34. if(res.code=="500"){
  35. vm.$message({
  36. message:'上传失败',
  37. type:'error'
  38. });
  39. }
  40. }
  41. })
  42. });
  43. new Vue({
  44. /**
  45. * 将vue中的this混入,在这里为了调用elementUI中的$message消息框
  46. */
  47. mixins: [vmMixins]
  48. })
  49. </script>
  50. </body>
  51. </html>

添加页面跳转接口Controller

在views/controller目录下创建页面跳转接口Controller:PageJumpController,并将下方代码复制进去。
pageControllerPath.png

  1. package com.ustcinfo.ishare.eip.todo.views.controller;
  2. import io.swagger.annotations.Api;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. @Controller
  7. @RequestMapping("/views/page")
  8. @Api(tags = "跳转文件上传测试页面接口")
  9. public class PageJumpController {
  10. /**
  11. * 跳转至文件上传页面
  12. * @return
  13. */
  14. @GetMapping("/fileUpload")
  15. public String toFileUpload(){
  16. return "upload/file_upload";
  17. }
  18. }

[!TIP]
如出现token验证问题无法跳转页面或访问接口,请尝试登陆系统后再进行页面访问和文件上传