multiUpload.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <el-form :model="form">
  3. <el-upload
  4. action
  5. :file-list="nFileList"
  6. :limit="limit"
  7. :auto-upload="true"
  8. list-type="listType"
  9. :on-exceed="handleExceed"
  10. :before-upload="handleBeforeUpload"
  11. :on-preview="handlePictureCardPreview"
  12. :on-remove="handleRemove"
  13. :http-request="handleUploadFile"
  14. >
  15. <el-button size="small" type="primary">点击上传</el-button>
  16. <span class="upload-tip"> ( 文件大小不超过{{ maxSize }}Mb )</span>
  17. </el-upload>
  18. <el-dialog :visible.sync="dialogVisible" :modal="false">
  19. <img width="100%" :src="dialogImageUrl" alt="">
  20. </el-dialog>
  21. </el-form>
  22. </template>
  23. <script>
  24. import { uploadFile } from '@/api/system/uploadApi'
  25. export default {
  26. props: {
  27. value: {
  28. type: Array,
  29. default: () => []
  30. },
  31. limit: {
  32. type: Number,
  33. default: 0
  34. },
  35. maxSize: {
  36. type: Number,
  37. default: 0
  38. },
  39. listType: {
  40. type: String,
  41. default: 'picture'
  42. },
  43. dirId: {
  44. type: Number,
  45. default: 0
  46. }
  47. },
  48. data() {
  49. return {
  50. dialogImageUrl: '',
  51. dialogVisible: false,
  52. oFileList: [],
  53. nFileList: [],
  54. limitNum: 1,
  55. form: {}
  56. }
  57. },
  58. watch: {
  59. value(val) {
  60. this.oFileList = val
  61. this.initFileList()
  62. },
  63. oFileList(val) {
  64. this.$emit('input', val)
  65. }
  66. },
  67. methods: {
  68. // 初始化列表
  69. initFileList() {
  70. this.nFileList = []
  71. this.oFileList.forEach((file) => {
  72. this.nFileList.push({ name: file.fileTitle, url: file.fileUrl, id: file.fileId })
  73. })
  74. },
  75. // 文件列表移除文件时的钩子
  76. handleRemove(file, fileList) {
  77. const fileId = file.id
  78. this.oFileList.forEach((file, index) => {
  79. if (file.opCode === 1 && fileId === file.fileId) {
  80. file.opCode = -1
  81. } else if (fileId === file.fileId) {
  82. this.oFileList.splice(index, 1)
  83. }
  84. })
  85. },
  86. // 上传文件之前的钩子
  87. handleBeforeUpload(file) {
  88. const size = file.size / 1024 / 1024 / 5
  89. if (size > 5) {
  90. this.$notify.warning({
  91. title: '警告',
  92. message: '图片大小必须小于5M'
  93. })
  94. }
  95. },
  96. // 文件超出个数限制时的钩子
  97. handleExceed(files, fileList) {
  98. },
  99. handleUploadFile({ file }) {
  100. const loading = this.$loading({
  101. lock: true,
  102. text: 'Loading',
  103. spinner: 'el-icon-loading',
  104. background: 'rgba(0, 0, 0, 0.7)'
  105. })
  106. const formData = new FormData()
  107. formData.append('file', file)
  108. formData.append('dirId', this.dirId)
  109. uploadFile(formData).then((resp) => {
  110. loading.close()
  111. const { code, data, msg } = resp
  112. if (code === 200) {
  113. const of = {}
  114. of.fileId = data.fileId
  115. of.fileTitle = data.fileTitle
  116. of.fileSize = data.fileSize
  117. of.fileUrl = data.fileUrl
  118. of.fileExt = data.fileExt
  119. this.oFileList.push(of)
  120. this.nFileList.push({ name: of.fileTitle, url: of.fileUrl, id: of.fileId })
  121. } else {
  122. this.$message.error(msg)
  123. }
  124. }).catch((error) => {
  125. loading.close()
  126. console.log(error)
  127. })
  128. },
  129. // 点击文件列表中已上传的文件时的钩子
  130. handlePictureCardPreview(file) {
  131. if (/\.(jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.url)) {
  132. this.dialogImageUrl = file.url
  133. this.dialogVisible = true
  134. return
  135. }
  136. window.open(file.url)
  137. }
  138. }
  139. }
  140. </script>
  141. <style lang="scss" scoped>
  142. .upload-tip {
  143. color: #fff;
  144. font-size: 10px;
  145. }
  146. </style>