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