index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <div class="upload-file">
  3. <el-upload
  4. :action="uploadFileUrl"
  5. :before-upload="handleBeforeUpload"
  6. :file-list="fileList"
  7. :limit="1"
  8. :on-error="handleUploadError"
  9. :on-exceed="handleExceed"
  10. :on-success="handleUploadSuccess"
  11. :show-file-list="false"
  12. :headers="headers"
  13. class="upload-file-uploader"
  14. ref="upload"
  15. >
  16. <!-- 上传按钮 -->
  17. <el-button size="mini" type="primary">选取文件</el-button>
  18. <!-- 上传提示 -->
  19. <div class="el-upload__tip" slot="tip" v-if="showTip">
  20. 请上传
  21. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  22. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  23. 的文件
  24. </div>
  25. </el-upload>
  26. <!-- 文件列表 -->
  27. <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
  28. <li :key="file.uid" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in list">
  29. <el-link :href="file.url" :underline="false" target="_blank">
  30. <span class="el-icon-document"> {{ getFileName(file.name) }} </span>
  31. </el-link>
  32. <div class="ele-upload-list__item-content-action">
  33. <el-link :underline="false" @click="handleDelete(index)" type="danger">删除</el-link>
  34. </div>
  35. </li>
  36. </transition-group>
  37. </div>
  38. </template>
  39. <script>
  40. import { getToken } from "@/utils/auth";
  41. export default {
  42. props: {
  43. // 值
  44. value: [String, Object, Array],
  45. // 大小限制(MB)
  46. fileSize: {
  47. type: Number,
  48. default: 5,
  49. },
  50. // 文件类型, 例如['png', 'jpg', 'jpeg']
  51. fileType: {
  52. type: Array,
  53. default: () => ["doc", "xls", "ppt", "txt", "pdf"],
  54. },
  55. // 是否显示提示
  56. isShowTip: {
  57. type: Boolean,
  58. default: true
  59. }
  60. },
  61. data() {
  62. return {
  63. uploadFileUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
  64. headers: {
  65. Authorization: "Bearer " + getToken(),
  66. },
  67. fileList: [],
  68. };
  69. },
  70. computed: {
  71. // 是否显示提示
  72. showTip() {
  73. return this.isShowTip && (this.fileType || this.fileSize);
  74. },
  75. // 列表
  76. list() {
  77. let temp = 1;
  78. if (this.value) {
  79. // 首先将值转为数组
  80. const list = Array.isArray(this.value) ? this.value : [this.value];
  81. // 然后将数组转为对象数组
  82. return list.map((item) => {
  83. if (typeof item === "string") {
  84. item = { name: item, url: item };
  85. }
  86. item.uid = item.uid || new Date().getTime() + temp++;
  87. return item;
  88. });
  89. } else {
  90. //上传后关闭弹出框再打开出现`只允许上传单个文件`提示,增加fileList清空可解决
  91. this.fileList = [];
  92. return [];
  93. }
  94. },
  95. },
  96. methods: {
  97. // 上传前校检格式和大小
  98. handleBeforeUpload(file) {
  99. // 校检文件类型
  100. if (this.fileType) {
  101. let fileExtension = "";
  102. if (file.name.lastIndexOf(".") > -1) {
  103. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  104. }
  105. const isTypeOk = this.fileType.some((type) => {
  106. if (file.type.indexOf(type) > -1) return true;
  107. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  108. return false;
  109. });
  110. if (!isTypeOk) {
  111. this.$message.error(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
  112. return false;
  113. }
  114. }
  115. // 校检文件大小
  116. if (this.fileSize) {
  117. const isLt = file.size / 1024 / 1024 < this.fileSize;
  118. if (!isLt) {
  119. this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
  120. return false;
  121. }
  122. }
  123. return true;
  124. },
  125. // 文件个数超出
  126. handleExceed() {
  127. this.$message.error(`只允许上传单个文件`);
  128. },
  129. // 上传失败
  130. handleUploadError(err) {
  131. this.$message.error("上传失败, 请重试");
  132. },
  133. // 上传成功回调
  134. handleUploadSuccess(res, file) {
  135. this.$message.success("上传成功");
  136. this.$emit("input", res.data.url);
  137. },
  138. // 删除文件
  139. handleDelete(index) {
  140. this.fileList.splice(index, 1);
  141. this.$emit("input", '');
  142. },
  143. // 获取文件名称
  144. getFileName(name) {
  145. if (name.lastIndexOf("/") > -1) {
  146. return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
  147. } else {
  148. return "";
  149. }
  150. }
  151. },
  152. created() {
  153. this.fileList = this.list;
  154. },
  155. };
  156. </script>
  157. <style scoped lang="scss">
  158. .upload-file-uploader {
  159. margin-bottom: 5px;
  160. }
  161. .upload-file-list .el-upload-list__item {
  162. border: 1px solid #e4e7ed;
  163. line-height: 2;
  164. margin-bottom: 10px;
  165. position: relative;
  166. }
  167. .upload-file-list .ele-upload-list__item-content {
  168. display: flex;
  169. justify-content: space-between;
  170. align-items: center;
  171. color: inherit;
  172. }
  173. .ele-upload-list__item-content-action .el-link {
  174. margin-right: 10px;
  175. }
  176. </style>