shangchuan.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>文本和图片上传</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. max-width: 400px;
  11. margin: 0 auto;
  12. padding: 20px;
  13. border: 1px solid #ccc;
  14. border-radius: 5px;
  15. background-color: #f9f9f9;
  16. }
  17. h2 {
  18. text-align: center;
  19. }
  20. label {
  21. display: block;
  22. margin: 10px 0 5px;
  23. }
  24. input[type="text"],
  25. input[type="file"] {
  26. width: 100%;
  27. padding: 8px;
  28. margin-bottom: 10px;
  29. border: 1px solid #ccc;
  30. border-radius: 4px;
  31. }
  32. button {
  33. width: 100%;
  34. padding: 10px;
  35. background-color: #007bff;
  36. color: white;
  37. border: none;
  38. border-radius: 4px;
  39. cursor: pointer;
  40. }
  41. button:hover {
  42. background-color: #0056b3;
  43. }
  44. img {
  45. max-width: 300px;
  46. display: block;
  47. margin: 20px auto;
  48. }
  49. #responseImage {
  50. display: none;
  51. margin-top: 20px;
  52. }
  53. /* Loading Overlay */
  54. .loading-overlay {
  55. position: fixed;
  56. top: 0;
  57. left: 0;
  58. width: 100%;
  59. height: 100%;
  60. background: rgba(255, 255, 255, 0.8);
  61. z-index: 1000;
  62. display: flex;
  63. justify-content: center;
  64. align-items: center;
  65. display: none;
  66. }
  67. .loading-spinner {
  68. border: 4px solid #007bff;
  69. border-top: 4px solid transparent;
  70. border-radius: 50%;
  71. width: 40px;
  72. height: 40px;
  73. animation: spin 1s linear infinite;
  74. }
  75. @keyframes spin {
  76. 0% { transform: rotate(0deg); }
  77. 100% { transform: rotate(360deg); }
  78. }
  79. </style>
  80. </head>
  81. <body>
  82. <h2>填写昵称和上传头像</h2>
  83. <form id="uploadForm">
  84. <div>
  85. <label for="textInput">输入昵称:</label>
  86. <input type="text" id="textInput" placeholder="请输入昵称" required />
  87. </div>
  88. <div>
  89. <label for="fileInput">上传头像:</label>
  90. <input type="file" id="fileInput" accept="image/*" required />
  91. </div>
  92. <button type="submit">提交</button>
  93. </form>
  94. <div id="previewContainer" style="display: none;">
  95. <h3>图片预览:</h3>
  96. <img id="imagePreview" alt="Uploaded Image" />
  97. </div>
  98. <img id="responseImage" alt="Response Image" />
  99. <!-- Loading Overlay -->
  100. <div id="loadingOverlay" class="loading-overlay">
  101. <div class="loading-spinner"></div>
  102. </div>
  103. <script>
  104. const form = document.getElementById('uploadForm');
  105. const textInput = document.getElementById('textInput');
  106. const fileInput = document.getElementById('fileInput');
  107. const previewContainer = document.getElementById('previewContainer');
  108. const imagePreview = document.getElementById('imagePreview');
  109. const responseImage = document.getElementById('responseImage');
  110. fileInput.addEventListener('change', function(event) {
  111. const file = event.target.files[0];
  112. if (file) {
  113. const reader = new FileReader();
  114. reader.onload = function(e) {
  115. imagePreview.src = e.target.result;
  116. previewContainer.style.display = 'block';
  117. };
  118. reader.readAsDataURL(file);
  119. }
  120. });
  121. form.addEventListener('submit', async function(event) {
  122. event.preventDefault();
  123. loadingOverlay.style.display = 'flex';
  124. const formData = new FormData();
  125. formData.append('text', textInput.value);
  126. formData.append('image', fileInput.files[0]);
  127. try {
  128. // const response = await fetch('http://zdl-admin.test/api/upload', {
  129. const response = await fetch('https://htnewa.jiuweiyun.cn/api/upload', {
  130. method: 'POST',
  131. body: formData,
  132. });
  133. if (response.ok) {
  134. const data = await response.json();
  135. console.log('上传成功:', data);
  136. // 假设接口返回一个图片URL
  137. responseImage.src = data.imageUrl; // 请根据实际返回的结构修改
  138. responseImage.style.display = 'block';
  139. } else {
  140. console.error('上传失败:', response.statusText);
  141. alert('上传失败,请重试!');
  142. }
  143. } catch (error) {
  144. console.error('网络错误:', error);
  145. alert('网络错误,请重试!');
  146. }
  147. loadingOverlay.style.display = 'none';
  148. // 重置表单
  149. textInput.value = '';
  150. fileInput.value = '';
  151. imagePreview.src = '';
  152. previewContainer.style.display = 'none';
  153. });
  154. </script>
  155. </body>
  156. </html>