upload.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <div>
  3. <el-upload
  4. :before-upload="checkFile"
  5. :headers="{'x-token':token}"
  6. :on-error="uploadError"
  7. :on-success="uploadSuccess"
  8. :show-file-list="false"
  9. action="/api/fileUploadAndDownload/upload"
  10. >
  11. <el-button size="small" type="primary">点击上传</el-button>
  12. <div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
  13. </el-upload>
  14. <el-table :data="tableData" border stripe>
  15. <el-table-column label="预览" width="100">
  16. <template slot-scope="scope">
  17. <img :alt="scope.row.alt" :src="scope.row.url" height="80" width="80" />
  18. </template>
  19. </el-table-column>
  20. <el-table-column label="日期" prop="UpdatedAt" width="180">
  21. <template slot-scope="scope">
  22. <div>
  23. {{scope.row.UpdatedAt|formatDate}}
  24. </div>
  25. </template>
  26. </el-table-column>
  27. <el-table-column label="文件名" prop="name" width="180"></el-table-column>
  28. <el-table-column label="链接" prop="url"></el-table-column>
  29. <el-table-column label="标签" prop="tag" width="100">
  30. <template slot-scope="scope">
  31. <el-tag
  32. :type="scope.row.tag === 'jpg' ? 'primary' : 'success'"
  33. disable-transitions
  34. >{{scope.row.tag}}</el-tag>
  35. </template>
  36. </el-table-column>
  37. <el-table-column label="操作" width="100">
  38. <template slot-scope="scope">
  39. <el-button @click="downloadFile(scope.row)" size="small" type="text">下载</el-button>
  40. <el-button @click="deleteFile(scope.row)" size="small" type="text">删除</el-button>
  41. </template>
  42. </el-table-column>
  43. </el-table>
  44. <el-pagination
  45. :current-page="page"
  46. :page-size="pageSize"
  47. :page-sizes="[10, 30, 50, 100]"
  48. :style="{float:'right',padding:'20px'}"
  49. :total="total"
  50. @current-change="handleCurrentChange"
  51. @size-change="handleSizeChange"
  52. layout="total, sizes, prev, pager, next, jumper"
  53. ></el-pagination>
  54. </div>
  55. </template>
  56. <script>
  57. import { mapGetters } from 'vuex'
  58. import infoList from '@/components/mixins/infoList'
  59. import { getFileList, deleteFile } from '@/api/fileUploadAndDownload'
  60. import { downloadImage } from '@/utils/downloadImg'
  61. import { formatTimeToStr } from '@/utils/data'
  62. export default {
  63. name: 'Upload',
  64. mixins: [infoList],
  65. data() {
  66. return {
  67. listApi: getFileList,
  68. listKey: 'list',
  69. tableData: [
  70. {
  71. UpdatedAt: '2019-10-25',
  72. name: '文件名.jpg',
  73. url: 'http://qmplusimg.henrongyi.top/1571321688timg.jpg',
  74. tag: 'jpg'
  75. },
  76. {
  77. UpdatedAt: '2019-10-25',
  78. name: '文件名.jpg',
  79. url: 'http://qmplusimg.henrongyi.top/157162774820191015140921496.gif',
  80. tag: 'gif'
  81. }
  82. ]
  83. }
  84. },
  85. computed: {
  86. ...mapGetters('user', ['userInfo', 'token'])
  87. },
  88. filters: {
  89. formatDate: function(time) {
  90. if (time != null && time != '') {
  91. var date = new Date(time)
  92. return formatTimeToStr(date, 'yyyy-MM-dd hh:mm:ss')
  93. } else {
  94. return ''
  95. }
  96. }
  97. },
  98. methods: {
  99. async deleteFile(row) {
  100. this.$confirm('此操作将永久删除所有角色下该菜单, 是否继续?', '提示', {
  101. confirmButtonText: '确定',
  102. cancelButtonText: '取消',
  103. type: 'warning'
  104. })
  105. .then(async () => {
  106. const res = await deleteFile(row)
  107. if (res.success) {
  108. this.$message({
  109. type: 'success',
  110. message: '删除成功!'
  111. })
  112. this.getTableData()
  113. }
  114. })
  115. .catch(() => {
  116. this.$message({
  117. type: 'info',
  118. message: '已取消删除'
  119. })
  120. })
  121. },
  122. checkFile(file) {
  123. const isJPG = file.type === 'image/jpeg'
  124. const isPng = file.type === 'image/png'
  125. const isLt2M = file.size / 1024 / 1024 < 2
  126. if (!isJPG && !isPng) {
  127. this.$message.error('上传头像图片只能是 JPG或png 格式!')
  128. }
  129. if (!isLt2M) {
  130. this.$message.error('上传头像图片大小不能超过 2MB!')
  131. }
  132. return (isPng || isJPG) && isLt2M
  133. },
  134. uploadSuccess(res) {
  135. this.$message({
  136. type: 'success',
  137. message: '上传成功'
  138. })
  139. if (res.success) {
  140. this.getTableData()
  141. }
  142. },
  143. uploadError() {
  144. this.$message({
  145. type: 'error',
  146. message: '上传失败'
  147. })
  148. },
  149. downloadFile(row) {
  150. downloadImage(row.url, row.name)
  151. }
  152. }
  153. }
  154. </script>