index.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>ESP32音乐播放器</title>
  5. <meta charset="UTF-8">
  6. <style>
  7. body {
  8. font-family: Arial, sans-serif;
  9. max-width: 800px;
  10. margin: 0 auto;
  11. padding: 20px;
  12. }
  13. .upload-form {
  14. border: 2px dashed #ccc;
  15. padding: 20px;
  16. margin: 20px 0;
  17. }
  18. .file-list {
  19. margin: 20px 0;
  20. }
  21. .file-item {
  22. display: flex;
  23. justify-content: space-between;
  24. padding: 10px;
  25. border-bottom: 1px solid #eee;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <h1>ESP32音乐播放器</h1>
  31. <div class="upload-form">
  32. <h2>上传音乐文件</h2>
  33. <form id="uploadForm">
  34. <input type="file" id="audioFile" accept="audio/*" required>
  35. <button type="submit">上传</button>
  36. </form>
  37. </div>
  38. <div class="file-list">
  39. <h2>已上传的音乐</h2>
  40. <div id="fileList">
  41. <!-- 文件列表将通过JavaScript动态填充 -->
  42. </div>
  43. </div>
  44. <script>
  45. document.getElementById('uploadForm').onsubmit = async (e) => {
  46. e.preventDefault();
  47. const formData = new FormData();
  48. const file = document.getElementById('audioFile').files[0];
  49. formData.append('file', file);
  50. try {
  51. const response = await fetch('/upload', {
  52. method: 'POST',
  53. body: formData
  54. });
  55. if (response.ok) {
  56. alert('文件上传成功');
  57. // 刷新文件列表
  58. loadFileList();
  59. } else {
  60. alert('上传失败');
  61. }
  62. } catch (error) {
  63. console.error('Error:', error);
  64. alert('上传出错');
  65. }
  66. };
  67. function playFile(filename) {
  68. fetch(`/play?file=${encodeURIComponent(filename)}`)
  69. .then(response => response.text())
  70. .then(result => alert(result))
  71. .catch(error => alert('播放失败'));
  72. }
  73. async function loadFileList() {
  74. try {
  75. const response = await fetch('/list');
  76. const files = await response.json();
  77. const fileList = document.getElementById('fileList');
  78. fileList.innerHTML = files.map(file => `
  79. <div class="file-item">
  80. <span>${file}</span>
  81. <button onclick="playFile('${file}')">播放</button>
  82. </div>
  83. `).join('');
  84. } catch (error) {
  85. console.error('Error:', error);
  86. }
  87. }
  88. // 页面加载时获取文件列表
  89. loadFileList();
  90. </script>
  91. </body>
  92. </html>