index.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. Object.values(row).forEach((value) => {
  31. const cell = document.createElement('td');
  32. cell.textContent = value;
  33. tableRow.appendChild(cell);
  34. });
  35. // Add the download button in the last column
  36. const downloadCell = document.createElement('td');
  37. const downloadButton = document.createElement('button');
  38. downloadButton.textContent = 'Download';
  39. downloadButton.onclick = () => downloadFile(row.categoryName); // Assuming row[1] is the filename
  40. downloadCell.appendChild(downloadButton);
  41. tableRow.appendChild(downloadCell);
  42. tableBody.appendChild(tableRow);
  43. });
  44. }
  45. async function downloadFile(filename) {
  46. const response = await fetch(`/download/${filename}`, {
  47. method: 'GET',
  48. headers: {
  49. 'Content-Type': 'application/json'
  50. }
  51. });
  52. if (response.ok) {
  53. // Create a blob from the response
  54. const blob = await response.blob();
  55. // Create a link element
  56. const link = document.createElement('a');
  57. link.href = URL.createObjectURL(blob);
  58. link.download = filename;
  59. // Append the link to the body (it needs to be in the DOM to work)
  60. document.body.appendChild(link);
  61. // Programmatically trigger the download
  62. link.click();
  63. // Clean up the DOM
  64. document.body.removeChild(link);
  65. } else {
  66. console.error('Failed to download the file');
  67. }
  68. }
  69. // Initial load
  70. document.addEventListener('DOMContentLoaded', fetchCategories);
  71. </script>
  72. <style>
  73. body {
  74. font-family: Arial, sans-serif;
  75. margin: 0;
  76. display: flex;
  77. }
  78. #sidebar {
  79. width: 20%;
  80. background-color: #f8f9fa;
  81. padding: 1em;
  82. border-right: 1px solid #ddd;
  83. }
  84. #content {
  85. flex-grow: 1;
  86. padding: 1em;
  87. }
  88. ul {
  89. list-style: none;
  90. padding: 0;
  91. }
  92. li {
  93. padding: 0.5em;
  94. cursor: pointer;
  95. border: 1px solid #ddd;
  96. margin-bottom: 0.5em;
  97. border-radius: 4px;
  98. text-align: center;
  99. }
  100. li:hover {
  101. background-color: #e9ecef;
  102. }
  103. table {
  104. width: 100%;
  105. border-collapse: collapse;
  106. }
  107. table, th, td {
  108. border: 1px solid #ddd;
  109. }
  110. th, td {
  111. padding: 0.5em;
  112. text-align: left;
  113. }
  114. th {
  115. background-color: #f1f1f1;
  116. }
  117. </style>
  118. </head>
  119. <body>
  120. <div id="sidebar">
  121. <h2>分类</h2>
  122. <ul id="category-list">
  123. <!-- Categories will be dynamically loaded here -->
  124. </ul>
  125. </div>
  126. <div id="content">
  127. <h2>数据</h2>
  128. <table>
  129. <thead>
  130. <tr>
  131. <th>ID</th>
  132. <th>名称</th>
  133. <th>URL</th>
  134. </tr>
  135. </thead>
  136. <tbody id="table-body">
  137. <!-- Table data will be dynamically loaded here -->
  138. </tbody>
  139. </table>
  140. </div>
  141. </body>
  142. </html>