index.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>文件管理系统</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. background-color: #f4f4f4;
  11. margin: 0;
  12. padding: 20px;
  13. }
  14. h1 {
  15. text-align: center;
  16. color: #333;
  17. }
  18. h2 {
  19. color: #555;
  20. margin-top: 20px;
  21. }
  22. ul {
  23. list-style-type: none;
  24. padding: 0;
  25. }
  26. li {
  27. background: #fff;
  28. margin: 5px 0;
  29. padding: 15px;
  30. border-radius: 5px;
  31. display: flex;
  32. justify-content: space-between;
  33. align-items: center;
  34. box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  35. }
  36. button {
  37. background-color: #28a745;
  38. color: white;
  39. border: none;
  40. padding: 8px 12px;
  41. border-radius: 5px;
  42. cursor: pointer;
  43. transition: background-color 0.3s;
  44. }
  45. button:hover {
  46. background-color: #218838;
  47. }
  48. input[type="file"], input[type="text"] {
  49. margin-top: 20px;
  50. display: block;
  51. width: 100%;
  52. padding: 10px;
  53. border: 1px solid #ccc;
  54. border-radius: 5px;
  55. }
  56. .upload-container {
  57. margin-top: 20px;
  58. }
  59. .pagination {
  60. margin-top: 20px;
  61. display: flex;
  62. justify-content: center;
  63. }
  64. .page-button {
  65. margin: 0 5px;
  66. padding: 8px 12px;
  67. border: 1px solid #ccc;
  68. border-radius: 5px;
  69. cursor: pointer;
  70. background: #007bff;
  71. color: white;
  72. transition: background-color 0.3s;
  73. }
  74. .page-button:hover {
  75. background-color: #0056b3;
  76. }
  77. .button-container {
  78. display: flex;
  79. gap: 10px;
  80. }
  81. </style>
  82. </head>
  83. <body>
  84. <h1>文件管理系统</h1>
  85. <h2>当前文件列表</h2>
  86. <input type="text" id="searchInput" placeholder="搜索文件..." />
  87. <button onclick="searchFiles()">搜索</button>
  88. <ul id="fileList"></ul>
  89. <div class="pagination" id="pagination"></div>
  90. <div class="upload-container">
  91. <input type="file" id="fileInput" />
  92. <button onclick="uploadFile()">上传文件</button>
  93. </div>
  94. <script>
  95. let currentPage = 0;
  96. const pageSize = 6;
  97. function loadFileList() {
  98. fetch(`/demo/api/files?page=${currentPage}&size=${pageSize}`)
  99. .then(response => response.json())
  100. .then(files => {
  101. const fileList = document.getElementById('fileList');
  102. fileList.innerHTML = '';
  103. files.forEach(file => {
  104. const li = document.createElement('li');
  105. li.innerHTML = `${file}
  106. <div class="button-container">
  107. <button onclick="downloadFile('${file}')">下载</button>
  108. <button onclick="deleteFile('${file}')">删除</button>
  109. </div>`;
  110. fileList.appendChild(li);
  111. });
  112. loadPagination();
  113. })
  114. .catch(error => console.error('Error:', error));
  115. }
  116. function uploadFile() {
  117. const fileInput = document.getElementById('fileInput');
  118. const file = fileInput.files[0];
  119. const formData = new FormData();
  120. formData.append('file', file);
  121. fetch('/demo/api/files/upload', {
  122. method: 'POST',
  123. body: formData
  124. })
  125. .then(response => response.text())
  126. .then(text => {
  127. alert(text);
  128. loadFileList();
  129. fileInput.value = '';
  130. })
  131. .catch(error => console.error('Error:', error));
  132. }
  133. function downloadFile(filename) {
  134. window.location.href = '/demo/api/files/download/' + filename;
  135. }
  136. function deleteFile(filename) {
  137. if (confirm(`确定要删除文件 ${filename} 吗?`)) {
  138. fetch(`/demo/api/files/delete/${filename}`, {
  139. method: 'DELETE'
  140. })
  141. .then(response => response.text())
  142. .then(text => {
  143. alert(text);
  144. loadFileList();
  145. })
  146. .catch(error => console.error('Error:', error));
  147. }
  148. }
  149. function searchFiles() {
  150. const searchQuery = document.getElementById('searchInput').value;
  151. fetch(`/demo/api/files?search=${searchQuery}&page=0&size=${pageSize}`)
  152. .then(response => response.json())
  153. .then(files => {
  154. const fileList = document.getElementById('fileList');
  155. fileList.innerHTML = '';
  156. files.forEach(file => {
  157. const li = document.createElement('li');
  158. li.innerHTML = `${file}
  159. <div class="button-container">
  160. <button onclick="downloadFile('${file}')">下载</button>
  161. <button onclick="deleteFile('${file}')">删除</button>
  162. </div>`;
  163. fileList.appendChild(li);
  164. });
  165. currentPage = 0;
  166. loadPagination();
  167. })
  168. .catch(error => console.error('Error:', error));
  169. }
  170. function loadPagination() {
  171. const pagination = document.getElementById('pagination');
  172. pagination.innerHTML = '';
  173. const prevButton = document.createElement('button');
  174. prevButton.innerHTML = '上一页';
  175. prevButton.classList.add('page-button');
  176. prevButton.onclick = () => {
  177. if (currentPage > 0) {
  178. currentPage--;
  179. loadFileList();
  180. }
  181. };
  182. pagination.appendChild(prevButton);
  183. const nextButton = document.createElement('button');
  184. nextButton.innerHTML = '下一页';
  185. nextButton.classList.add('page-button');
  186. nextButton.onclick = () => {
  187. currentPage++;
  188. loadFileList();
  189. };
  190. pagination.appendChild(nextButton);
  191. }
  192. // 页面加载时加载文件列表
  193. loadFileList();
  194. </script>
  195. </body>
  196. </html>