test.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <template>
  2. <div class="test-container">
  3. <h2>文件上传测试页面</h2>
  4. <p>这个页面用于测试文件上传功能,可以在浏览器中直接访问</p>
  5. <div class="test-info">
  6. <h3>测试说明:</h3>
  7. <ul>
  8. <li>此页面可以在任何浏览器中访问</li>
  9. <li>文件选择功能会正常工作</li>
  10. <li>提交功能会模拟发送数据</li>
  11. <li>实际使用时需要在微信小程序中打开</li>
  12. </ul>
  13. </div>
  14. <div class="upload-area" @click="triggerFileInput">
  15. <div class="upload-icon">
  16. <i class="el-icon-upload"></i>
  17. </div>
  18. <div class="upload-text">
  19. <p>点击选择文件</p>
  20. <p class="upload-hint">支持图片、文档、视频等格式</p>
  21. </div>
  22. <input
  23. ref="fileInput"
  24. type="file"
  25. multiple
  26. accept="image/*,.pdf,.doc,.docx,.xls,.xlsx,.txt,.mp4,.mp3"
  27. @change="handleFileSelect"
  28. style="display: none"
  29. />
  30. </div>
  31. <div v-if="selectedFiles.length > 0" class="file-list">
  32. <h3>已选择的文件</h3>
  33. <div class="file-item" v-for="(file, index) in selectedFiles" :key="index">
  34. <div class="file-info">
  35. <div class="file-icon">
  36. <i :class="getFileIcon(file.type)"></i>
  37. </div>
  38. <div class="file-details">
  39. <div class="file-name">{{ file.name }}</div>
  40. <div class="file-size">{{ formatFileSize(file.size) }}</div>
  41. </div>
  42. </div>
  43. <div class="file-actions">
  44. <el-button
  45. type="danger"
  46. size="mini"
  47. @click="removeFile(index)"
  48. icon="el-icon-delete"
  49. >
  50. 删除
  51. </el-button>
  52. </div>
  53. </div>
  54. </div>
  55. <div class="submit-area">
  56. <el-button
  57. type="primary"
  58. size="large"
  59. @click="testSubmit"
  60. :disabled="selectedFiles.length === 0"
  61. :loading="submitting"
  62. >
  63. {{ submitting ? '提交中...' : '测试提交' }}
  64. </el-button>
  65. </div>
  66. <div v-if="testResult" class="test-result">
  67. <h3>测试结果:</h3>
  68. <pre>{{ testResult }}</pre>
  69. </div>
  70. </div>
  71. </template>
  72. <script>
  73. export default {
  74. name: 'FileUploadTest',
  75. data() {
  76. return {
  77. selectedFiles: [],
  78. submitting: false,
  79. testResult: ''
  80. }
  81. },
  82. methods: {
  83. triggerFileInput() {
  84. this.$refs.fileInput.click()
  85. },
  86. handleFileSelect(event) {
  87. const files = Array.from(event.target.files)
  88. files.forEach(file => {
  89. if (file.size > 50 * 1024 * 1024) {
  90. this.$message.error(`文件 ${file.name} 超过50MB限制`)
  91. return
  92. }
  93. const exists = this.selectedFiles.find(f => f.name === file.name)
  94. if (exists) {
  95. this.$message.warning(`文件 ${file.name} 已存在`)
  96. return
  97. }
  98. this.selectedFiles.push(file)
  99. })
  100. event.target.value = ''
  101. },
  102. removeFile(index) {
  103. this.selectedFiles.splice(index, 1)
  104. },
  105. getFileIcon(type) {
  106. if (type.startsWith('image/')) {
  107. return 'el-icon-picture'
  108. } else if (type.includes('pdf')) {
  109. return 'el-icon-document'
  110. } else if (type.includes('word') || type.includes('document')) {
  111. return 'el-icon-document'
  112. } else if (type.includes('excel') || type.includes('spreadsheet')) {
  113. return 'el-icon-document'
  114. } else if (type.includes('video')) {
  115. return 'el-icon-video-camera'
  116. } else if (type.includes('audio')) {
  117. return 'el-icon-headset'
  118. } else {
  119. return 'el-icon-document'
  120. }
  121. },
  122. formatFileSize(bytes) {
  123. if (bytes === 0) return '0 B'
  124. const k = 1024
  125. const sizes = ['B', 'KB', 'MB', 'GB']
  126. const i = Math.floor(Math.log(bytes) / Math.log(k))
  127. return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
  128. },
  129. async testSubmit() {
  130. if (this.selectedFiles.length === 0) {
  131. this.$message.warning('请先选择文件')
  132. return
  133. }
  134. this.submitting = true
  135. this.testResult = ''
  136. try {
  137. const filesData = await this.prepareFilesData()
  138. // 模拟提交数据
  139. const mockResult = {
  140. success: true,
  141. message: '测试提交成功',
  142. timestamp: new Date().toISOString(),
  143. filesCount: filesData.length,
  144. files: filesData.map(file => ({
  145. name: file.name,
  146. type: file.type,
  147. size: file.size,
  148. dataLength: file.data.length
  149. }))
  150. }
  151. this.testResult = JSON.stringify(mockResult, null, 2)
  152. this.$message.success('测试提交成功')
  153. // 清空文件列表
  154. this.selectedFiles = []
  155. } catch (error) {
  156. console.error('测试提交失败:', error)
  157. this.$message.error('测试提交失败')
  158. this.testResult = JSON.stringify({ error: error.message }, null, 2)
  159. } finally {
  160. this.submitting = false
  161. }
  162. },
  163. async prepareFilesData() {
  164. const filesData = []
  165. for (const file of this.selectedFiles) {
  166. try {
  167. const base64 = await this.fileToBase64(file)
  168. filesData.push({
  169. name: file.name,
  170. type: file.type,
  171. size: file.size,
  172. data: base64
  173. })
  174. } catch (error) {
  175. console.error('转换文件失败:', error)
  176. }
  177. }
  178. return filesData
  179. },
  180. fileToBase64(file) {
  181. return new Promise((resolve, reject) => {
  182. const reader = new FileReader()
  183. reader.onload = () => resolve(reader.result)
  184. reader.onerror = reject
  185. reader.readAsDataURL(file)
  186. })
  187. }
  188. }
  189. }
  190. </script>
  191. <style scoped>
  192. .test-container {
  193. max-width: 800px;
  194. margin: 0 auto;
  195. padding: 20px;
  196. background: #fff;
  197. min-height: 100vh;
  198. }
  199. .test-info {
  200. background: #f0f9ff;
  201. border: 1px solid #b3d8ff;
  202. border-radius: 6px;
  203. padding: 15px;
  204. margin-bottom: 20px;
  205. }
  206. .test-info h3 {
  207. margin-top: 0;
  208. color: #409eff;
  209. }
  210. .test-info ul {
  211. margin: 10px 0;
  212. padding-left: 20px;
  213. }
  214. .test-info li {
  215. margin: 5px 0;
  216. color: #606266;
  217. }
  218. .upload-area {
  219. border: 2px dashed #d9d9d9;
  220. border-radius: 8px;
  221. padding: 40px;
  222. text-align: center;
  223. cursor: pointer;
  224. transition: all 0.3s;
  225. background: #fafafa;
  226. margin-bottom: 20px;
  227. }
  228. .upload-area:hover {
  229. border-color: #409eff;
  230. background: #f0f9ff;
  231. }
  232. .upload-icon {
  233. font-size: 48px;
  234. color: #c0c4cc;
  235. margin-bottom: 20px;
  236. }
  237. .upload-text p {
  238. margin: 5px 0;
  239. color: #606266;
  240. }
  241. .upload-hint {
  242. font-size: 12px;
  243. color: #c0c4cc;
  244. }
  245. .file-list {
  246. margin: 20px 0;
  247. }
  248. .file-list h3 {
  249. margin-bottom: 15px;
  250. color: #303133;
  251. }
  252. .file-item {
  253. display: flex;
  254. align-items: center;
  255. justify-content: space-between;
  256. padding: 15px;
  257. border: 1px solid #ebeef5;
  258. border-radius: 6px;
  259. margin-bottom: 10px;
  260. background: #fff;
  261. transition: all 0.3s;
  262. }
  263. .file-item:hover {
  264. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  265. }
  266. .file-info {
  267. display: flex;
  268. align-items: center;
  269. flex: 1;
  270. }
  271. .file-icon {
  272. font-size: 24px;
  273. color: #409eff;
  274. margin-right: 15px;
  275. }
  276. .file-details {
  277. flex: 1;
  278. }
  279. .file-name {
  280. font-weight: 500;
  281. color: #303133;
  282. margin-bottom: 5px;
  283. word-break: break-all;
  284. }
  285. .file-size {
  286. font-size: 12px;
  287. color: #909399;
  288. }
  289. .file-actions {
  290. margin-left: 15px;
  291. }
  292. .submit-area {
  293. margin: 20px 0;
  294. text-align: center;
  295. }
  296. .test-result {
  297. margin-top: 20px;
  298. background: #f5f5f5;
  299. border-radius: 6px;
  300. padding: 15px;
  301. }
  302. .test-result h3 {
  303. margin-top: 0;
  304. color: #303133;
  305. }
  306. .test-result pre {
  307. background: #fff;
  308. border: 1px solid #ddd;
  309. border-radius: 4px;
  310. padding: 10px;
  311. overflow-x: auto;
  312. font-size: 12px;
  313. line-height: 1.4;
  314. }
  315. @media (max-width: 768px) {
  316. .test-container {
  317. padding: 15px;
  318. }
  319. .upload-area {
  320. padding: 30px 20px;
  321. }
  322. .file-item {
  323. flex-direction: column;
  324. align-items: flex-start;
  325. }
  326. .file-actions {
  327. margin-left: 0;
  328. margin-top: 10px;
  329. align-self: flex-end;
  330. }
  331. }
  332. </style>