index.vue 6.6 KB

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