index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <div class="upload-file">
  3. <el-upload
  4. :action="uploadFileUrl"
  5. :before-upload="handleBeforeUpload"
  6. :file-list="fileList"
  7. :limit="limit"
  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.url" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
  29. <el-link :href="`${baseUrl}${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. name: "FileUpload",
  43. props: {
  44. // 值
  45. value: [String, Object, Array],
  46. // 数量限制
  47. limit: {
  48. type: Number,
  49. default: 5,
  50. },
  51. // 大小限制(MB)
  52. fileSize: {
  53. type: Number,
  54. default: 5,
  55. },
  56. // 文件类型, 例如['png', 'jpg', 'jpeg']
  57. fileType: {
  58. type: Array,
  59. default: () => ["doc", "xls", "ppt", "txt", "pdf"],
  60. },
  61. // 是否显示提示
  62. isShowTip: {
  63. type: Boolean,
  64. default: true
  65. }
  66. },
  67. data() {
  68. return {
  69. baseUrl: process.env.VUE_APP_BASE_API,
  70. uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
  71. headers: {
  72. Authorization: "Bearer " + getToken(),
  73. },
  74. fileList: [],
  75. };
  76. },
  77. watch: {
  78. value: {
  79. handler(val) {
  80. if (val) {
  81. let temp = 1;
  82. // 首先将值转为数组
  83. const list = Array.isArray(val) ? val : this.value.split(',');
  84. // 然后将数组转为对象数组
  85. this.fileList = list.map(item => {
  86. if (typeof item === "string") {
  87. item = { name: item, url: item };
  88. }
  89. item.uid = item.uid || new Date().getTime() + temp++;
  90. return item;
  91. });
  92. } else {
  93. this.fileList = [];
  94. return [];
  95. }
  96. },
  97. deep: true,
  98. immediate: true
  99. }
  100. },
  101. computed: {
  102. // 是否显示提示
  103. showTip() {
  104. return this.isShowTip && (this.fileType || this.fileSize);
  105. },
  106. },
  107. methods: {
  108. // 上传前校检格式和大小
  109. handleBeforeUpload(file) {
  110. // 校检文件类型
  111. if (this.fileType) {
  112. let fileExtension = "";
  113. if (file.name.lastIndexOf(".") > -1) {
  114. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  115. }
  116. const isTypeOk = this.fileType.some((type) => {
  117. if (file.type.indexOf(type) > -1) return true;
  118. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  119. return false;
  120. });
  121. if (!isTypeOk) {
  122. this.$message.error(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
  123. return false;
  124. }
  125. }
  126. // 校检文件大小
  127. if (this.fileSize) {
  128. const isLt = file.size / 1024 / 1024 < this.fileSize;
  129. if (!isLt) {
  130. this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
  131. return false;
  132. }
  133. }
  134. return true;
  135. },
  136. // 文件个数超出
  137. handleExceed() {
  138. this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
  139. },
  140. // 上传失败
  141. handleUploadError(err) {
  142. this.$message.error("上传失败, 请重试");
  143. },
  144. // 上传成功回调
  145. handleUploadSuccess(res, file) {
  146. this.$message.success("上传成功");
  147. this.fileList.push({ name: res.fileName, url: res.fileName });
  148. this.$emit("input", this.listToString(this.fileList));
  149. },
  150. // 删除文件
  151. handleDelete(index) {
  152. this.fileList.splice(index, 1);
  153. this.$emit("input", this.listToString(this.fileList));
  154. },
  155. // 获取文件名称
  156. getFileName(name) {
  157. if (name.lastIndexOf("/") > -1) {
  158. return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
  159. } else {
  160. return "";
  161. }
  162. },
  163. // 对象转成指定字符串分隔
  164. listToString(list, separator) {
  165. let strs = "";
  166. separator = separator || ",";
  167. for (let i in list) {
  168. strs += list[i].url + separator;
  169. }
  170. return strs != '' ? strs.substr(0, strs.length - 1) : '';
  171. }
  172. }
  173. };
  174. </script>
  175. <style scoped lang="scss">
  176. .upload-file-uploader {
  177. margin-bottom: 5px;
  178. }
  179. .upload-file-list .el-upload-list__item {
  180. border: 1px solid #e4e7ed;
  181. line-height: 2;
  182. margin-bottom: 10px;
  183. position: relative;
  184. }
  185. .upload-file-list .ele-upload-list__item-content {
  186. display: flex;
  187. justify-content: space-between;
  188. align-items: center;
  189. color: inherit;
  190. }
  191. .ele-upload-list__item-content-action .el-link {
  192. margin-right: 10px;
  193. }
  194. </style>