index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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" class="select_file" @blur.native="choose">选取文件</el-button>
  18. <!-- 上传提示 -->
  19. <div class="el-upload__tip" slot="tip" v-if="showTip">
  20. 请上传
  21. <template v-if="fileSize"> 大小不超过 <b style="color: #13CE66 ">{{ fileSize }}MB</b></template>
  22. <template v-if="fileType"> 格式为 <b style="color: #13CE66">{{ 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="`${baseUrl}${file.url}`" :underline="false" target="_blank">
  30. <span class="el-icon-document"> {{ 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" class="del"></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. names: '',
  68. },
  69. data() {
  70. return {
  71. baseUrl: process.env.VUE_APP_BASE_API,
  72. uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
  73. headers: {
  74. Authorization: "Bearer " + getToken(),
  75. },
  76. fileList: [],
  77. faith: null
  78. };
  79. },
  80. watch: {
  81. value: {
  82. handler(val) {
  83. if (val) {
  84. let temp = 1;
  85. // 首先将值转为数组
  86. const list = Array.isArray(val) ? val : this.value.split(',');
  87. // 然后将数组转为对象数组
  88. this.fileList = list.map(item => {
  89. if (typeof item === "string") {
  90. item = {name: item, url: item};
  91. }
  92. item.uid = item.uid || new Date().getTime() + temp++;
  93. return item;
  94. });
  95. } else {
  96. this.fileList = [];
  97. return [];
  98. }
  99. },
  100. deep: true,
  101. immediate: true,
  102. }
  103. },
  104. computed: {
  105. // 是否显示提示
  106. showTip() {
  107. return this.isShowTip && (this.fileType || this.fileSize);
  108. },
  109. },
  110. methods: {
  111. choose() {
  112. console.log(this.names, 5555)
  113. this.$emit("names", this.fileList)
  114. },
  115. // 上传前校检格式和大小
  116. handleBeforeUpload(file) {
  117. // 校检文件类型
  118. if (this.fileType) {
  119. let fileExtension = "";
  120. if (file.name.lastIndexOf(".") > -1) {
  121. fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
  122. }
  123. const isTypeOk = this.fileType.some((type) => {
  124. if (file.type.indexOf(type) > -1) return true;
  125. if (fileExtension && fileExtension.indexOf(type) > -1) return true;
  126. return false;
  127. });
  128. if (!isTypeOk) {
  129. this.$message.error(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
  130. return false;
  131. }
  132. }
  133. // 校检文件大小
  134. if (this.fileSize) {
  135. const isLt = file.size / 1024 / 1024 < this.fileSize;
  136. if (!isLt) {
  137. this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
  138. return false;
  139. }
  140. }
  141. return true;
  142. },
  143. // 文件个数超出
  144. handleExceed() {
  145. this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
  146. },
  147. // 上传失败
  148. handleUploadError(err) {
  149. this.$message.error("上传失败, 请重试");
  150. },
  151. // 上传成功回调
  152. handleUploadSuccess(res, file) {
  153. this.$message.success("上传成功");
  154. this.fileList.push({name: file.name, url: res.fileName,});
  155. console.log(this.fileList)
  156. this.$emit("input", this.fileList);
  157. },
  158. // 删除文件
  159. handleDelete(index) {
  160. this.fileList.splice(index, 1);
  161. this.$emit("input", this.listToString(this.fileList));
  162. },
  163. // 获取文件名称
  164. getFileName(name) {
  165. if (name.lastIndexOf("/") > -1) {
  166. return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
  167. } else {
  168. return "";
  169. }
  170. },
  171. // 对象转成指定字符串分隔
  172. listToString(list, separator) {
  173. let strs = "";
  174. separator = separator || ",";
  175. for (let i in list) {
  176. strs += list[i].url + separator;
  177. }
  178. return strs != '' ? strs.substr(0, strs.length - 1) : '';
  179. }
  180. }
  181. };
  182. </script>
  183. <style scoped lang="scss">
  184. .upload-file-uploader {
  185. margin-bottom: 5px;
  186. }
  187. .upload-file-list .el-upload-list__item {
  188. border: 1px solid #e4e7ed;
  189. line-height: 2;
  190. margin-bottom: 10px;
  191. position: relative;
  192. display: flex;
  193. }
  194. .upload-file-list .ele-upload-list__item-content {
  195. display: flex;
  196. justify-content: space-between;
  197. align-items: center;
  198. color: inherit;
  199. }
  200. .ele-upload-list__item-content-action .el-link {
  201. margin-right: 10px;
  202. }
  203. // 选取文件
  204. .select_file {
  205. width: 70px;
  206. height: 30px;
  207. display: flex;
  208. justify-content: center;
  209. align-items: center;
  210. }
  211. .el-upload__tip {
  212. margin-left: -15px;
  213. }
  214. // 删除
  215. .del {
  216. width: 23px;
  217. height: 23px;
  218. left: 40px;
  219. background-image: url("../../images/delicon.png");
  220. background-repeat: no-repeat;
  221. background-color: #FF4949;
  222. background-position: center;
  223. margin-bottom: 2px;
  224. margin-left: -5px;
  225. }
  226. </style>