| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>文件管理系统</title>
- <script>
- // Fetch categories from the backend
- async function fetchCategories() {
- const response = await fetch('/categories'); // Backend endpoint to get categories
- const categories = await response.json();
- const categoryList = document.getElementById('category-list');
- // Populate the left-side list with categories
- categoryList.innerHTML = '';
- categories.forEach(category => {
- const listItem = document.createElement('li');
- listItem.textContent = category.categoryName;
- listItem.onclick = () => fetchTableData(category.categoryName);
- categoryList.appendChild(listItem);
- });
- }
- async function fetchTableData(tableName) {
- const response = await fetch(`/category-data?categoryName=${tableName}`); // Backend endpoint for table data
- const tableData = await response.json();
- const tableBody = document.getElementById('table-body');
- // Populate the right-side table with data
- tableBody.innerHTML = '';
- tableData.forEach(row => {
- const tableRow = document.createElement('tr');
- Object.values(row).forEach((value) => {
- const cell = document.createElement('td');
- cell.textContent = value;
- tableRow.appendChild(cell);
- });
- // Add the download button in the last column
- const downloadCell = document.createElement('td');
- const downloadButton = document.createElement('button');
- downloadButton.textContent = 'Download';
- downloadButton.onclick = () => downloadFile(row.categoryName); // Assuming row[1] is the filename
- downloadCell.appendChild(downloadButton);
- tableRow.appendChild(downloadCell);
- tableBody.appendChild(tableRow);
- });
- }
- async function downloadFile(filename) {
- const response = await fetch(`/download/${filename}`, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json'
- }
- });
- if (response.ok) {
- // Create a blob from the response
- const blob = await response.blob();
- // Create a link element
- const link = document.createElement('a');
- link.href = URL.createObjectURL(blob);
- link.download = filename;
- // Append the link to the body (it needs to be in the DOM to work)
- document.body.appendChild(link);
- // Programmatically trigger the download
- link.click();
- // Clean up the DOM
- document.body.removeChild(link);
- } else {
- console.error('Failed to download the file');
- }
- }
- // Initial load
- document.addEventListener('DOMContentLoaded', fetchCategories);
- </script>
- <style>
- body {
- font-family: Arial, sans-serif;
- margin: 0;
- display: flex;
- }
- #sidebar {
- width: 20%;
- background-color: #f8f9fa;
- padding: 1em;
- border-right: 1px solid #ddd;
- }
- #content {
- flex-grow: 1;
- padding: 1em;
- }
- ul {
- list-style: none;
- padding: 0;
- }
- li {
- padding: 0.5em;
- cursor: pointer;
- border: 1px solid #ddd;
- margin-bottom: 0.5em;
- border-radius: 4px;
- text-align: center;
- }
- li:hover {
- background-color: #e9ecef;
- }
- table {
- width: 100%;
- border-collapse: collapse;
- }
- table, th, td {
- border: 1px solid #ddd;
- }
- th, td {
- padding: 0.5em;
- text-align: left;
- }
- th {
- background-color: #f1f1f1;
- }
- </style>
- </head>
- <body>
- <div id="sidebar">
- <h2>分类</h2>
- <ul id="category-list">
- <!-- Categories will be dynamically loaded here -->
- </ul>
- </div>
- <div id="content">
- <h2>数据</h2>
- <table>
- <thead>
- <tr>
- <th>ID</th>
- <th>名称</th>
- <th>URL</th>
- </tr>
- </thead>
- <tbody id="table-body">
- <!-- Table data will be dynamically loaded here -->
- </tbody>
- </table>
- </div>
- </body>
- </html>
|