index.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>文件管理系统</title>
  7. <script>
  8. // Fetch categories from the backend
  9. async function fetchCategories() {
  10. const response = await fetch('/categories'); // Backend endpoint to get categories
  11. const categories = await response.json();
  12. const categoryList = document.getElementById('category-list');
  13. // Populate the left-side list with categories
  14. categoryList.innerHTML = '';
  15. categories.forEach(category => {
  16. const listItem = document.createElement('li');
  17. listItem.textContent = category.categoryName;
  18. listItem.onclick = () => fetchTableData(category.categoryName);
  19. categoryList.appendChild(listItem);
  20. });
  21. }
  22. async function fetchTableData(tableName) {
  23. const response = await fetch(`/category-data?categoryName=${tableName}`); // Backend endpoint for table data
  24. const tableData = await response.json();
  25. const tableBody = document.getElementById('table-body');
  26. // Populate the right-side table with data
  27. tableBody.innerHTML = '';
  28. tableData.forEach(row => {
  29. const tableRow = document.createElement('tr');
  30. // 显示 id 和 categoryName
  31. const cells = [row.id, row.categoryName];
  32. cells.forEach(value => {
  33. const cell = document.createElement('td');
  34. cell.textContent = value;
  35. tableRow.appendChild(cell);
  36. });
  37. // Add the download button in the last column
  38. const downloadCell = document.createElement('td');
  39. const downloadButton = document.createElement('button');
  40. downloadButton.textContent = 'Download';
  41. downloadButton.onclick = () => downloadFile(row.categoryName); // Assuming row[1] is the filename
  42. downloadCell.appendChild(downloadButton);
  43. tableRow.appendChild(downloadCell);
  44. tableBody.appendChild(tableRow);
  45. });
  46. }
  47. async function downloadFile(filename) {
  48. const response = await fetch(`/download/${filename}`, {
  49. method: 'GET',
  50. headers: {
  51. 'Content-Type': 'application/json'
  52. }
  53. });
  54. if (response.ok) {
  55. // Create a blob from the response
  56. const blob = await response.blob();
  57. // Create a link element
  58. const link = document.createElement('a');
  59. link.href = URL.createObjectURL(blob);
  60. link.download = filename;
  61. // Append the link to the body (it needs to be in the DOM to work)
  62. document.body.appendChild(link);
  63. // Programmatically trigger the download
  64. link.click();
  65. // Clean up the DOM
  66. document.body.removeChild(link);
  67. } else {
  68. console.error('Failed to download the file');
  69. }
  70. }
  71. // Function to handle file upload
  72. async function uploadFile() {
  73. const fileInput = document.getElementById('file-input');
  74. const file = fileInput.files[0]; // Get the selected file
  75. const formData = new FormData();
  76. formData.append('file', file);
  77. const response = await fetch('/upload', {
  78. method: 'POST',
  79. body: formData,
  80. });
  81. if (response.ok) {
  82. alert('File uploaded successfully!');
  83. fetchCategories(); // Reload categories after upload
  84. } else if (response.status === 409) {
  85. // 处理文件已存在的情况
  86. const data = await response.json();
  87. alert(data.message); // 显示 "File already exists, no need to insert again."
  88. } else {
  89. alert('Failed to upload file');
  90. }
  91. }
  92. // Initial load
  93. document.addEventListener('DOMContentLoaded', fetchCategories);
  94. </script>
  95. <style>
  96. body {
  97. font-family: Arial, sans-serif;
  98. margin: 0;
  99. display: flex;
  100. }
  101. #sidebar {
  102. width: 20%;
  103. background-color: #f8f9fa;
  104. padding: 1em;
  105. border-right: 1px solid #ddd;
  106. display: flex;
  107. flex-direction: column;
  108. justify-content: flex-start;
  109. height: 100vh;
  110. }
  111. #content {
  112. flex-grow: 1;
  113. padding: 1em;
  114. }
  115. ul {
  116. list-style: none;
  117. padding: 0;
  118. }
  119. li {
  120. padding: 0.5em;
  121. cursor: pointer;
  122. border: 1px solid #ddd;
  123. margin-bottom: 0.5em;
  124. border-radius: 4px;
  125. text-align: center;
  126. }
  127. li:hover {
  128. background-color: #e9ecef;
  129. }
  130. table {
  131. width: 100%;
  132. border-collapse: collapse;
  133. }
  134. table, th, td {
  135. border: 1px solid #ddd;
  136. }
  137. th, td {
  138. padding: 0.5em;
  139. text-align: left;
  140. }
  141. th {
  142. background-color: #f1f1f1;
  143. }
  144. /* Styling the upload section to be at the bottom of the sidebar */
  145. #upload-section {
  146. margin-top: auto;
  147. padding: 10px;
  148. background-color: #fff;
  149. border: 1px solid #ddd;
  150. border-radius: 5px;
  151. }
  152. #upload-section input[type="file"] {
  153. width: calc(100% - 120px);
  154. margin-right: 10px;
  155. padding: 5px;
  156. font-size: 16px;
  157. }
  158. #upload-section button {
  159. padding: 8px 16px;
  160. font-size: 16px;
  161. background-color: #007bff;
  162. color: white;
  163. border: none;
  164. border-radius: 5px;
  165. cursor: pointer;
  166. }
  167. #upload-section button:hover {
  168. background-color: #0056b3;
  169. }
  170. </style>
  171. </head>
  172. <body>
  173. <div id="sidebar">
  174. <h2>分类</h2>
  175. <ul id="category-list">
  176. <!-- Categories will be dynamically loaded here -->
  177. </ul>
  178. <!-- File Upload Section (Now positioned at the bottom of the sidebar) -->
  179. <div id="upload-section">
  180. <h3>上传文件</h3>
  181. <input type="file" id="file-input">
  182. <button onclick="uploadFile()">上传</button>
  183. </div>
  184. </div>
  185. <div id="content">
  186. <h2>文件列表</h2>
  187. <table>
  188. <thead>
  189. <tr>
  190. <th>ID</th>
  191. <th>Category Name</th>
  192. <th>操作</th>
  193. </tr>
  194. </thead>
  195. <tbody id="table-body">
  196. <!-- Data will be dynamically loaded here -->
  197. </tbody>
  198. </table>
  199. </div>
  200. </body>
  201. </html>