upload.jsp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: EDZ
  4. Date: 2021/6/28
  5. Time: 11:08
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
  9. <%@include file="/context/mytags.jsp" %>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <title>模板导入</title>
  14. <t:base type="jquery,easyui,tools,DatePicker"></t:base>
  15. <style>
  16. .loading{width:95px;height:7px;font-size:7px;margin:5px auto;background:url(http://static.tieba.baidu.com/tb/editor/images/loading.gif) no-repeat}
  17. </style>
  18. <script type="text/javascript">
  19. //编写自定义JS代码
  20. </script>
  21. </head>
  22. <body>
  23. <t:formvalid formid="formobj" dialog="true" usePlugin="password" layout="table" beforeSubmit="postData">
  24. <div class="loading upload-icon" style="display:none;position: absolute;left: 150px;top: 50%"></div>
  25. <table style="width: 200px;" cellpadding="0" cellspacing="1" class="formtable">
  26. <tr>
  27. <td align="right">
  28. <label class="Validform_label">
  29. 选择文件:
  30. </label>
  31. </td>
  32. <td class="value">
  33. <input type="file" id="file" multiple="multiple" onchange="handleFile()">
  34. <span class="Validform_checktip"></span>
  35. <label class="Validform_label" style="display: none;">名称</label>
  36. </td>
  37. </tr>
  38. </table>
  39. </t:formvalid>
  40. </body>
  41. <script>
  42. /**
  43. * $icon: 文件上传中loading图标
  44. * fs: 上传的文件($("#file")[0].files)
  45. * max_size: 文件大小的最大值(1024 * 1024 * 100为100M)
  46. */
  47. var formData,fs;
  48. function handleFile() {
  49. formData = new FormData(),
  50. fs = $("#file")[0].files;
  51. let max_size = 1024 * 1024 * 30;
  52. for (let i = 0; i < fs.length; i++) {
  53. let d = fs[0]
  54. if(d.size <= max_size){ //文件必须小于30M
  55. if(/.(xls|XLS|xlsx|XLSX)$/.test(d.name)){ //文件必须为文档
  56. formData.append("files", fs[i]); //文件上传处理
  57. }else{
  58. alert('上传文件必须是XLS!')
  59. return false
  60. }
  61. }else{
  62. alert('上传文件过大!')
  63. return false
  64. }
  65. }
  66. }
  67. function postData() {
  68. let $icon = $(".upload-icon");
  69. $icon.show();
  70. $.ajax({
  71. url:'./carController.do?importExcel', /*接口域名地址*/
  72. type:'post',
  73. data: formData,
  74. contentType: false,
  75. processData: false,
  76. success:function(res){
  77. //{"success":true,"msg":"文件导入失败!","obj":null,"attributes":null,"jsonStr":"{\"msg\":\"文件导入失败!\",\"success\":true}"}
  78. W.tip(res.msg);
  79. $icon.hide();
  80. if(res.success){
  81. W.reloadTable();
  82. windowapi.close();
  83. }else{
  84. console.log(res);
  85. }
  86. }
  87. })
  88. }
  89. </script>
  90. </html>