index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <div class="upload-file">
  3. <el-upload
  4. multiple
  5. :action="uploadFileUrl"
  6. :before-upload="handleBeforeUpload"
  7. :file-list="fileList"
  8. :data="data"
  9. :limit="limit"
  10. :on-error="handleUploadError"
  11. :on-exceed="handleExceed"
  12. :on-success="handleUploadSuccess"
  13. :show-file-list="false"
  14. :headers="headers"
  15. class="upload-file-uploader"
  16. ref="fileUpload"
  17. v-if="!disabled"
  18. >
  19. <!-- 上传按钮 -->
  20. <el-button size="mini" type="primary">选取文件</el-button>
  21. <!-- 上传提示 -->
  22. <div class="el-upload__tip" slot="tip" v-if="showTip">
  23. 请上传
  24. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  25. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  26. 的文件
  27. </div>
  28. </el-upload>
  29. <!-- 文件列表 -->
  30. <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
  31. <li :key="file.url" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
  32. <el-link :href="file.url" :underline="false" target="_blank">
  33. <span class="el-icon-document"> {{ getFileName(file.name) }} </span>
  34. </el-link>
  35. <div class="ele-upload-list__item-content-action">
  36. <el-link :underline="false" @click="handleDelete(index)" type="danger" v-if="!disabled">删除</el-link>
  37. </div>
  38. </li>
  39. </transition-group>
  40. </div>
  41. </template>
  42. <script>
  43. import { getToken } from "@/utils/auth"
  44. export default {
  45. name: "FileUpload",
  46. props: {
  47. // 值
  48. value: [String, Object, Array],
  49. // 上传接口地址
  50. action: {
  51. type: String,
  52. default: "/file/upload"
  53. },
  54. // 上传携带的参数
  55. data: {
  56. type: Object
  57. },
  58. // 数量限制
  59. limit: {
  60. type: Number,
  61. default: 5
  62. },
  63. // 大小限制(MB)
  64. fileSize: {
  65. type: Number,
  66. default: 5
  67. },
  68. // 文件类型, 例如['png', 'jpg', 'jpeg']
  69. fileType: {
  70. type: Array,
  71. default: () => ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "pdf"]
  72. },
  73. // 是否显示提示
  74. isShowTip: {
  75. type: Boolean,
  76. default: true
  77. },
  78. // 禁用组件(仅查看文件)
  79. disabled: {
  80. type: Boolean,
  81. default: false
  82. }
  83. },
  84. data() {
  85. return {
  86. number: 0,
  87. uploadList: [],
  88. uploadFileUrl: process.env.VUE_APP_BASE_API + this.action, // 上传文件服务器地址
  89. headers: {
  90. Authorization: "Bearer " + getToken(),
  91. },
  92. fileList: [],
  93. }
  94. },
  95. watch: {
  96. value: {
  97. handler(val) {
  98. if (val) {
  99. let temp = 1
  100. // 首先将值转为数组
  101. const list = Array.isArray(val) ? val : this.value.split(',')
  102. // 然后将数组转为对象数组
  103. this.fileList = list.map(item => {
  104. if (typeof item === "string") {
  105. item = { name: item, url: item }
  106. }
  107. item.uid = item.uid || new Date().getTime() + temp++
  108. return item
  109. })
  110. } else {
  111. this.fileList = []
  112. return []
  113. }
  114. },
  115. deep: true,
  116. immediate: true
  117. }
  118. },
  119. computed: {
  120. // 是否显示提示
  121. showTip() {
  122. return this.isShowTip && (this.fileType || this.fileSize)
  123. },
  124. },
  125. methods: {
  126. // 上传前校检格式和大小
  127. handleBeforeUpload(file) {
  128. // 校检文件类型
  129. if (this.fileType) {
  130. const fileName = file.name.split('.')
  131. const fileExt = fileName[fileName.length - 1]
  132. const isTypeOk = this.fileType.indexOf(fileExt) >= 0
  133. if (!isTypeOk) {
  134. this.$modal.msgError(`文件格式不正确,请上传${this.fileType.join("/")}格式文件!`)
  135. return false
  136. }
  137. }
  138. // 校检文件名是否包含特殊字符
  139. if (file.name.includes(',')) {
  140. this.$modal.msgError('文件名不正确,不能包含英文逗号!')
  141. return false
  142. }
  143. // 校检文件大小
  144. if (this.fileSize) {
  145. const isLt = file.size / 1024 / 1024 < this.fileSize
  146. if (!isLt) {
  147. this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`)
  148. return false
  149. }
  150. }
  151. this.$modal.loading("正在上传文件,请稍候...")
  152. this.number++
  153. return true
  154. },
  155. // 文件个数超出
  156. handleExceed() {
  157. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`)
  158. },
  159. // 上传失败
  160. handleUploadError(err) {
  161. this.$modal.msgError("上传文件失败,请重试")
  162. this.$modal.closeLoading()
  163. },
  164. // 上传成功回调
  165. handleUploadSuccess(res, file) {
  166. if (res.code === 200) {
  167. this.uploadList.push({ name: res.data.url, url: res.data.url })
  168. this.uploadedSuccessfully()
  169. } else {
  170. this.number--
  171. this.$modal.closeLoading()
  172. this.$modal.msgError(res.msg)
  173. this.$refs.fileUpload.handleRemove(file)
  174. this.uploadedSuccessfully()
  175. }
  176. },
  177. // 删除文件
  178. handleDelete(index) {
  179. this.fileList.splice(index, 1)
  180. this.$emit("input", this.listToString(this.fileList))
  181. },
  182. // 上传结束处理
  183. uploadedSuccessfully() {
  184. if (this.number > 0 && this.uploadList.length === this.number) {
  185. this.fileList = this.fileList.concat(this.uploadList)
  186. this.uploadList = []
  187. this.number = 0
  188. this.$emit("input", this.listToString(this.fileList))
  189. this.$modal.closeLoading()
  190. }
  191. },
  192. // 获取文件名称
  193. getFileName(name) {
  194. // 如果是url那么取最后的名字 如果不是直接返回
  195. if (name.lastIndexOf("/") > -1) {
  196. return name.slice(name.lastIndexOf("/") + 1)
  197. } else {
  198. return name
  199. }
  200. },
  201. // 对象转成指定字符串分隔
  202. listToString(list, separator) {
  203. let strs = ""
  204. separator = separator || ","
  205. for (let i in list) {
  206. strs += list[i].url + separator
  207. }
  208. return strs != '' ? strs.substr(0, strs.length - 1) : ''
  209. }
  210. }
  211. }
  212. </script>
  213. <style scoped lang="scss">
  214. .upload-file-uploader {
  215. margin-bottom: 5px;
  216. }
  217. .upload-file-list .el-upload-list__item {
  218. border: 1px solid #e4e7ed;
  219. line-height: 2;
  220. margin-bottom: 10px;
  221. position: relative;
  222. }
  223. .upload-file-list .ele-upload-list__item-content {
  224. display: flex;
  225. justify-content: space-between;
  226. align-items: center;
  227. color: inherit;
  228. }
  229. .ele-upload-list__item-content-action .el-link {
  230. margin-right: 10px;
  231. }
  232. </style>