index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 + "/common/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. return [];
  91. }
  92. },
  93. },
  94. methods: {
  95. // 上传前校检格式和大小
  96. handleBeforeUpload(file) {
  97. // 校检文件类型
  98. if (this.fileType) {
  99. let fileExtension = "";
  100. if (file.name.lastIndexOf(".") > -1) {
  101. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  102. }
  103. const isTypeOk = this.fileType.some((type) => {
  104. if (file.type.indexOf(type) > -1) return true;
  105. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  106. return false;
  107. });
  108. if (!isTypeOk) {
  109. this.$message.error(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
  110. return false;
  111. }
  112. }
  113. // 校检文件大小
  114. if (this.fileSize) {
  115. const isLt = file.size / 1024 / 1024 < this.fileSize;
  116. if (!isLt) {
  117. this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
  118. return false;
  119. }
  120. }
  121. return true;
  122. },
  123. // 文件个数超出
  124. handleExceed() {
  125. this.$message.error(`只允许上传单个文件`);
  126. },
  127. // 上传失败
  128. handleUploadError(err) {
  129. this.$message.error("上传失败, 请重试");
  130. },
  131. // 上传成功回调
  132. handleUploadSuccess(res, file) {
  133. this.$message.success("上传成功");
  134. this.$emit("input", res.url);
  135. },
  136. // 删除文件
  137. handleDelete(index) {
  138. this.fileList.splice(index, 1);
  139. this.$emit("input", '');
  140. },
  141. // 获取文件名称
  142. getFileName(name) {
  143. if (name.lastIndexOf("/") > -1) {
  144. return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
  145. } else {
  146. return "";
  147. }
  148. }
  149. },
  150. created() {
  151. this.fileList = this.list;
  152. },
  153. };
  154. </script>
  155. <style scoped lang="scss">
  156. .upload-file-uploader {
  157. margin-bottom: 5px;
  158. }
  159. .upload-file-list .el-upload-list__item {
  160. border: 1px solid #e4e7ed;
  161. line-height: 2;
  162. margin-bottom: 10px;
  163. position: relative;
  164. }
  165. .upload-file-list .ele-upload-list__item-content {
  166. display: flex;
  167. justify-content: space-between;
  168. align-items: center;
  169. color: inherit;
  170. }
  171. .ele-upload-list__item-content-action .el-link {
  172. margin-right: 10px;
  173. }
  174. </style>