作者 RuoYi

FileUpload组件支持多文件上传

@@ -4,9 +4,7 @@ @@ -4,9 +4,7 @@
4 :action="uploadFileUrl" 4 :action="uploadFileUrl"
5 :before-upload="handleBeforeUpload" 5 :before-upload="handleBeforeUpload"
6 :file-list="fileList" 6 :file-list="fileList"
7 - :limit="1"  
8 :on-error="handleUploadError" 7 :on-error="handleUploadError"
9 - :on-exceed="handleExceed"  
10 :on-success="handleUploadSuccess" 8 :on-success="handleUploadSuccess"
11 :show-file-list="false" 9 :show-file-list="false"
12 :headers="headers" 10 :headers="headers"
@@ -26,8 +24,8 @@ @@ -26,8 +24,8 @@
26 24
27 <!-- 文件列表 --> 25 <!-- 文件列表 -->
28 <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul"> 26 <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
29 - <li :key="file.uid" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in list">  
30 - <el-link :href="file.url" :underline="false" target="_blank"> 27 + <li :key="file.uid" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
  28 + <el-link :href="`${baseUrl}${file.url}`" :underline="false" target="_blank">
31 <span class="el-icon-document"> {{ getFileName(file.name) }} </span> 29 <span class="el-icon-document"> {{ getFileName(file.name) }} </span>
32 </el-link> 30 </el-link>
33 <div class="ele-upload-list__item-content-action"> 31 <div class="ele-upload-list__item-content-action">
@@ -42,6 +40,7 @@ @@ -42,6 +40,7 @@
42 import { getToken } from "@/utils/auth"; 40 import { getToken } from "@/utils/auth";
43 41
44 export default { 42 export default {
  43 + name: "FileUpload",
45 props: { 44 props: {
46 // 值 45 // 值
47 value: [String, Object, Array], 46 value: [String, Object, Array],
@@ -63,6 +62,7 @@ export default { @@ -63,6 +62,7 @@ export default {
63 }, 62 },
64 data() { 63 data() {
65 return { 64 return {
  65 + baseUrl: process.env.VUE_APP_BASE_API,
66 uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 66 uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
67 headers: { 67 headers: {
68 Authorization: "Bearer " + getToken(), 68 Authorization: "Bearer " + getToken(),
@@ -70,30 +70,35 @@ export default { @@ -70,30 +70,35 @@ export default {
70 fileList: [], 70 fileList: [],
71 }; 71 };
72 }, 72 },
  73 + watch: {
  74 + value: {
  75 + handler(val) {
  76 + if (val) {
  77 + let temp = 1;
  78 + // 首先将值转为数组
  79 + const list = Array.isArray(val) ? val : this.value.split(',');
  80 + // 然后将数组转为对象数组
  81 + this.fileList = list.map(item => {
  82 + if (typeof item === "string") {
  83 + item = { name: item, url: item };
  84 + }
  85 + item.uid = item.uid || new Date().getTime() + temp++;
  86 + return item;
  87 + });
  88 + } else {
  89 + this.fileList = [];
  90 + return [];
  91 + }
  92 + },
  93 + deep: true,
  94 + immediate: true
  95 + }
  96 + },
73 computed: { 97 computed: {
74 // 是否显示提示 98 // 是否显示提示
75 showTip() { 99 showTip() {
76 return this.isShowTip && (this.fileType || this.fileSize); 100 return this.isShowTip && (this.fileType || this.fileSize);
77 }, 101 },
78 - // 列表  
79 - list() {  
80 - let temp = 1;  
81 - if (this.value) {  
82 - // 首先将值转为数组  
83 - const list = Array.isArray(this.value) ? this.value : [this.value];  
84 - // 然后将数组转为对象数组  
85 - return 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 }, 102 },
98 methods: { 103 methods: {
99 // 上传前校检格式和大小 104 // 上传前校检格式和大小
@@ -124,10 +129,6 @@ export default { @@ -124,10 +129,6 @@ export default {
124 } 129 }
125 return true; 130 return true;
126 }, 131 },
127 - // 文件个数超出  
128 - handleExceed() {  
129 - this.$message.error(`只允许上传单个文件`);  
130 - },  
131 // 上传失败 132 // 上传失败
132 handleUploadError(err) { 133 handleUploadError(err) {
133 this.$message.error("上传失败, 请重试"); 134 this.$message.error("上传失败, 请重试");
@@ -135,12 +136,13 @@ export default { @@ -135,12 +136,13 @@ export default {
135 // 上传成功回调 136 // 上传成功回调
136 handleUploadSuccess(res, file) { 137 handleUploadSuccess(res, file) {
137 this.$message.success("上传成功"); 138 this.$message.success("上传成功");
138 - this.$emit("input", res.url); 139 + this.fileList.push({ name: res.fileName, url: res.fileName });
  140 + this.$emit("input", this.listToString(this.fileList));
139 }, 141 },
140 // 删除文件 142 // 删除文件
141 handleDelete(index) { 143 handleDelete(index) {
142 this.fileList.splice(index, 1); 144 this.fileList.splice(index, 1);
143 - this.$emit("input", ''); 145 + this.$emit("input", this.listToString(this.fileList));
144 }, 146 },
145 // 获取文件名称 147 // 获取文件名称
146 getFileName(name) { 148 getFileName(name) {
@@ -149,11 +151,16 @@ export default { @@ -149,11 +151,16 @@ export default {
149 } else { 151 } else {
150 return ""; 152 return "";
151 } 153 }
  154 + },
  155 + // 对象转成分隔字符串
  156 + listToString(list) {
  157 + let files = "";
  158 + for (let key in list) {
  159 + files += list[key].url + ",";
  160 + }
  161 + return files.substr(0, files.length - 1);
152 } 162 }
153 - },  
154 - created() {  
155 - this.fileList = this.list;  
156 - }, 163 + }
157 }; 164 };
158 </script> 165 </script>
159 166