index.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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}`);
  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. // Display id and 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);
  42. downloadCell.appendChild(downloadButton);
  43. // Add the delete button in the last column
  44. const deleteCell = document.createElement('td');
  45. const deleteButton = document.createElement('button');
  46. deleteButton.textContent = 'Delete';
  47. deleteButton.onclick = () => deleteRow(tableName, row.id); // Pass the table name and row id
  48. deleteCell.appendChild(deleteButton);
  49. tableRow.appendChild(downloadCell);
  50. tableRow.appendChild(deleteCell);
  51. tableBody.appendChild(tableRow);
  52. });
  53. }
  54. async function downloadFile(filename) {
  55. const response = await fetch(`/download/${filename}`, {
  56. method: 'GET',
  57. headers: {
  58. 'Content-Type': 'application/json'
  59. }
  60. });
  61. if (response.ok) {
  62. // Create a blob from the response
  63. const blob = await response.blob();
  64. // Create a link element
  65. const link = document.createElement('a');
  66. link.href = URL.createObjectURL(blob);
  67. link.download = filename;
  68. // Append the link to the body (it needs to be in the DOM to work)
  69. document.body.appendChild(link);
  70. // Programmatically trigger the download
  71. link.click();
  72. // Clean up the DOM
  73. document.body.removeChild(link);
  74. } else {
  75. console.error('Failed to download the file');
  76. }
  77. }
  78. // Function to handle file upload
  79. async function uploadFile() {
  80. const fileInput = document.getElementById('file-input');
  81. const file = fileInput.files[0]; // Get the selected file
  82. const formData = new FormData();
  83. formData.append('file', file);
  84. const response = await fetch('/upload', {
  85. method: 'POST',
  86. body: formData,
  87. });
  88. if (response.ok) {
  89. alert('File uploaded successfully!');
  90. fetchCategories(); // Reload categories after upload
  91. } else if (response.status === 409) {
  92. // 处理文件已存在的情况
  93. const data = await response.json();
  94. alert(data.message); // 显示 "File already exists, no need to insert again."
  95. } else {
  96. alert('Failed to upload file');
  97. }
  98. }
  99. // Function to handle file deletion
  100. // Function to handle delete
  101. async function deleteRow(tableName, rowId) {
  102. const confirmation = confirm('Are you sure you want to delete this row?');
  103. if (confirmation) {
  104. const response = await fetch(`/delete_files?tableName=${tableName}&rowId=${rowId}`, {
  105. method: 'DELETE',
  106. });
  107. if (response.ok) {
  108. alert('Row deleted successfully!');
  109. fetchTableData(tableName); // Reload the table data after deletion
  110. } else {
  111. alert('Failed to delete the row');
  112. }
  113. }
  114. }
  115. // Initial load
  116. document.addEventListener('DOMContentLoaded', fetchCategories);
  117. </script>
  118. <style>
  119. body {
  120. font-family: Arial, sans-serif;
  121. margin: 0;
  122. display: flex;
  123. }
  124. #sidebar {
  125. width: 20%;
  126. background-color: #f8f9fa;
  127. padding: 1em;
  128. border-right: 1px solid #ddd;
  129. display: flex;
  130. flex-direction: column;
  131. justify-content: flex-start;
  132. height: 100vh;
  133. }
  134. #content {
  135. flex-grow: 1;
  136. padding: 1em;
  137. }
  138. ul {
  139. list-style: none;
  140. padding: 0;
  141. }
  142. li {
  143. padding: 0.5em;
  144. cursor: pointer;
  145. border: 1px solid #ddd;
  146. margin-bottom: 0.5em;
  147. border-radius: 4px;
  148. text-align: center;
  149. }
  150. li:hover {
  151. background-color: #e9ecef;
  152. }
  153. table {
  154. width: 100%;
  155. border-collapse: collapse;
  156. }
  157. table, th, td {
  158. border: 1px solid #ddd;
  159. }
  160. th, td {
  161. padding: 0.5em;
  162. text-align: left;
  163. }
  164. th {
  165. background-color: #f1f1f1;
  166. }
  167. /* Styling the upload section to be at the bottom of the sidebar */
  168. #upload-section {
  169. margin-top: auto;
  170. padding: 10px;
  171. background-color: #fff;
  172. border: 1px solid #ddd;
  173. border-radius: 5px;
  174. }
  175. #upload-section input[type="file"] {
  176. width: calc(100% - 120px);
  177. margin-right: 10px;
  178. padding: 5px;
  179. font-size: 16px;
  180. }
  181. #upload-section button {
  182. padding: 8px 16px;
  183. font-size: 16px;
  184. background-color: #007bff;
  185. color: white;
  186. border: none;
  187. border-radius: 5px;
  188. cursor: pointer;
  189. }
  190. #upload-section button:hover {
  191. background-color: #0056b3;
  192. }
  193. </style>
  194. </head>
  195. <body>
  196. <div id="sidebar">
  197. <h2>分类</h2>
  198. <ul id="category-list">
  199. <!-- Categories will be dynamically loaded here -->
  200. </ul>
  201. <!-- File Upload Section (Now positioned at the bottom of the sidebar) -->
  202. <div id="upload-section">
  203. <h3>上传文件</h3>
  204. <input type="file" id="file-input">
  205. <button onclick="uploadFile()">上传</button>
  206. </div>
  207. </div>
  208. <div id="content">
  209. <h2>文件列表</h2>
  210. <table>
  211. <thead>
  212. <tr>
  213. <th>ID</th>
  214. <th>文件名</th>
  215. <th>操作</th>
  216. <th>删除</th> <!-- Added Delete column -->
  217. </tr>
  218. </thead>
  219. <tbody id="table-body">
  220. <!-- Data will be dynamically loaded here -->
  221. </tbody>
  222. </table>
  223. </div>
  224. </body>
  225. </html>