| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <!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');
- // 显示 id 和 categoryName
- const cells = [row.id, row.categoryName];
- cells.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');
- }
- }
- // Function to handle file upload
- async function uploadFile() {
- const fileInput = document.getElementById('file-input');
- const file = fileInput.files[0]; // Get the selected file
- const formData = new FormData();
- formData.append('file', file);
- const response = await fetch('/upload', {
- method: 'POST',
- body: formData,
- });
- if (response.ok) {
- alert('File uploaded successfully!');
- fetchCategories(); // Reload categories after upload
- } else if (response.status === 409) {
- // 处理文件已存在的情况
- const data = await response.json();
- alert(data.message); // 显示 "File already exists, no need to insert again."
- } else {
- alert('Failed to upload 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;
- display: flex;
- flex-direction: column;
- justify-content: flex-start;
- height: 100vh;
- }
- #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;
- }
- /* Styling the upload section to be at the bottom of the sidebar */
- #upload-section {
- margin-top: auto;
- padding: 10px;
- background-color: #fff;
- border: 1px solid #ddd;
- border-radius: 5px;
- }
- #upload-section input[type="file"] {
- width: calc(100% - 120px);
- margin-right: 10px;
- padding: 5px;
- font-size: 16px;
- }
- #upload-section button {
- padding: 8px 16px;
- font-size: 16px;
- background-color: #007bff;
- color: white;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- }
- #upload-section button:hover {
- background-color: #0056b3;
- }
- </style>
- </head>
- <body>
- <div id="sidebar">
- <h2>分类</h2>
- <ul id="category-list">
- <!-- Categories will be dynamically loaded here -->
- </ul>
- <!-- File Upload Section (Now positioned at the bottom of the sidebar) -->
- <div id="upload-section">
- <h3>上传文件</h3>
- <input type="file" id="file-input">
- <button onclick="uploadFile()">上传</button>
- </div>
- </div>
- <div id="content">
- <h2>文件列表</h2>
- <table>
- <thead>
- <tr>
- <th>ID</th>
- <th>Category Name</th>
- <th>操作</th>
- </tr>
- </thead>
- <tbody id="table-body">
- <!-- Data will be dynamically loaded here -->
- </tbody>
- </table>
- </div>
- </body>
- </html>
|