firmware.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. {% extends "base.html" %}
  2. {% block content %}
  3. <section class="firmware-upload">
  4. <h2>上传固件</h2>
  5. {% if error %}
  6. <div class="error-message">{{ error }}</div>
  7. {% endif %}
  8. <form method="POST" enctype="multipart/form-data" id="upload-form">
  9. {% if firmwares %}
  10. <input type="hidden" id="existing-filenames" value="{% for firmware in firmwares %}{{ firmware.filename }}{% if not loop.last %},{% endif %}{% endfor %}">
  11. {% else %}
  12. <input type="hidden" id="existing-filenames" value="">
  13. {% endif %}
  14. <div class="form-group">
  15. <label for="file">选择固件文件</label>
  16. <input type="file" id="file" name="file" accept=".bin,.zip,.tar,.rar" required>
  17. <small>支持的格式: bin, zip, tar, rar</small>
  18. </div>
  19. <div class="form-group">
  20. <label for="remark">备注信息</label>
  21. <textarea id="remark" name="remark" required placeholder="请输入固件描述信息"></textarea>
  22. <small>备注信息不能为空</small>
  23. </div>
  24. <button type="submit" class="upload-btn">上传固件</button>
  25. </form>
  26. </section>
  27. <script>
  28. document.addEventListener('DOMContentLoaded', function() {
  29. // 获取现有文件名列表
  30. const existingFilenamesInput = document.getElementById('existing-filenames');
  31. const existingFilenames = existingFilenamesInput.value ? existingFilenamesInput.value.split(',') : [];
  32. // 过滤掉空字符串
  33. const validFilenames = existingFilenames.filter(filename => filename.trim() !== '');
  34. // 获取文件输入框和表单
  35. const fileInput = document.getElementById('file');
  36. const uploadForm = document.getElementById('upload-form');
  37. // 监听文件选择事件
  38. fileInput.addEventListener('change', function() {
  39. if (this.files.length > 0) {
  40. const selectedFilename = this.files[0].name;
  41. // 检查文件名是否重复
  42. if (validFilenames.includes(selectedFilename)) {
  43. alert(`文件名 "${selectedFilename}" 已存在,请更改文件名后再上传`);
  44. this.value = ''; // 清除文件选择
  45. }
  46. }
  47. });
  48. // 表单提交前再次检查
  49. uploadForm.addEventListener('submit', function(e) {
  50. if (fileInput.files.length > 0) {
  51. const selectedFilename = fileInput.files[0].name;
  52. if (validFilenames.includes(selectedFilename)) {
  53. e.preventDefault(); // 阻止表单提交
  54. alert(`文件名 "${selectedFilename}" 已存在,请更改文件名后再上传`);
  55. }
  56. }
  57. });
  58. });
  59. </script>
  60. <section class="firmware-list">
  61. <h2>固件信息</h2>
  62. <table class="data-table">
  63. <thead>
  64. <tr>
  65. <th>文件名</th>
  66. <th>文件大小</th>
  67. <th>上传时间</th>
  68. <th>备注</th>
  69. <th>操作</th>
  70. </tr>
  71. </thead>
  72. <tbody>
  73. {% for firmware in firmwares %}
  74. <tr>
  75. <td>{{ firmware.filename }}</td>
  76. <td>{{ firmware.filesize }}</td>
  77. <td>{{ firmware.upload_time }}</td>
  78. <td>{{ firmware.remark }}</td>
  79. <td class="actions">
  80. <button class="edit-btn" data-id="{{ firmware.id }}" data-remark="{{ firmware.remark }}">编辑</button>
  81. <a href="{{ url_for('firmware', download=firmware.id) }}" class="download-btn">下载</a>
  82. <a href="{{ url_for('firmware', delete=firmware.id) }}" class="delete-btn" onclick="return confirm('确定要删除吗?')">删除</a>
  83. </td>
  84. </tr>
  85. {% endfor %}
  86. </tbody>
  87. </table>
  88. </section>
  89. <!-- 编辑备注模态框 -->
  90. <div id="edit-modal" class="modal">
  91. <div class="modal-content">
  92. <span class="close-btn">&times;</span>
  93. <h3>编辑备注</h3>
  94. <form id="edit-form" method="GET" action="{{ url_for('firmware') }}">
  95. <input type="hidden" id="edit-id" name="edit">
  96. <div class="form-group">
  97. <label for="new-remark">备注信息</label>
  98. <textarea id="new-remark" name="remark" required></textarea>
  99. </div>
  100. <button type="submit" class="save-btn">保存</button>
  101. </form>
  102. </div>
  103. </div>
  104. {% endblock %}
  105. {% block scripts %}
  106. <script>
  107. // 编辑功能实现
  108. const modal = document.getElementById('edit-modal');
  109. const editBtns = document.querySelectorAll('.edit-btn');
  110. const closeBtn = document.querySelector('.close-btn');
  111. const editForm = document.getElementById('edit-form');
  112. const editId = document.getElementById('edit-id');
  113. const newRemark = document.getElementById('new-remark');
  114. editBtns.forEach(btn => {
  115. btn.addEventListener('click', () => {
  116. editId.value = btn.getAttribute('data-id');
  117. newRemark.value = btn.getAttribute('data-remark');
  118. modal.style.display = 'block';
  119. });
  120. });
  121. closeBtn.addEventListener('click', () => {
  122. modal.style.display = 'none';
  123. });
  124. window.addEventListener('click', (e) => {
  125. if (e.target == modal) {
  126. modal.style.display = 'none';
  127. }
  128. });
  129. </script>
  130. {% endblock %}