upload-component-v2.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <section>
  3. <el-col :span="8">
  4. <el-upload
  5. :disabled="display"
  6. class="upload-demo"
  7. ref="upload"
  8. :multiple="multiple"
  9. action="#"
  10. :accept="accept"
  11. :on-preview="handlePreview"
  12. :on-remove="handleRemove"
  13. :on-change="handleChange"
  14. :file-list="fileList"
  15. :limit="limit"
  16. :on-exceed="handleExceed"
  17. :http-request="handleUpload"
  18. :auto-upload="false"
  19. >
  20. <el-button v-show="!display" slot="trigger" size="small" type="primary"
  21. >选取文件</el-button
  22. >
  23. <el-button
  24. v-show="!display"
  25. style="margin-left: 10px"
  26. size="small"
  27. type="success"
  28. @click="submitUpload"
  29. >开始上传</el-button
  30. >
  31. </el-upload>
  32. </el-col>
  33. <!-- 图片预览 -->
  34. <el-dialog
  35. title="图片预览"
  36. :append-to-body="true"
  37. :visible.sync="previewVisible"
  38. width="50%"
  39. >
  40. <img
  41. :src="previewPath"
  42. :alt="previewName"
  43. style="width: 100%; height: 100%"
  44. />
  45. </el-dialog>
  46. </section>
  47. </template>
  48. <script>
  49. import { uploadUrl, downloadUrl, uploadFiles } from "@/api/file";
  50. import { getFileExt } from "@/api/util";
  51. export default {
  52. name: "upload-component-v2",
  53. props: {
  54. title: {
  55. type: String,
  56. default: "附件",
  57. },
  58. accept: {
  59. type: String,
  60. default: "",
  61. },
  62. multiple: {
  63. type: Boolean,
  64. default: false,
  65. },
  66. limit: {
  67. type: Number,
  68. default: 5,
  69. },
  70. display: {
  71. type: Boolean,
  72. default: false,
  73. },
  74. fileObjList: {
  75. type: Array,
  76. default: () => [],
  77. },
  78. displayTitle: {
  79. type: Boolean,
  80. default: true,
  81. },
  82. displayStar: {
  83. type: Boolean,
  84. default: true,
  85. },
  86. value: {
  87. type: Array,
  88. default: () => [],
  89. },
  90. },
  91. watch: {
  92. fileList(newVal) {
  93. this.$emit("uploadSuccess", newVal);
  94. },
  95. fileObjList(newVal) {
  96. this.fileList = newVal;
  97. },
  98. },
  99. data() {
  100. return {
  101. fileList: [],
  102. uploadUrl: uploadUrl,
  103. previewPath: "",
  104. previewName: "",
  105. previewVisible: false,
  106. uploadFileList: [], //已经上传过的文件列表
  107. };
  108. },
  109. methods: {
  110. handleUpload(file) {
  111. function getBase64(file) {
  112. return new Promise((resolve, reject) => {
  113. const reader = new FileReader();
  114. reader.readAsDataURL(file);
  115. reader.onload = () => resolve(reader.result);
  116. reader.onerror = (error) => reject(error);
  117. });
  118. }
  119. return getBase64(file.file).then((res) => {
  120. //需要return才会显示上传成功状态,不加return不好使
  121. // 开始上传
  122. const formData = new FormData();
  123. formData.append("file", file.file);
  124. uploadFiles(formData).then(({ data }) => {
  125. if (data && data.code === "200") {
  126. data.data.forEach((item) => {
  127. let fileData = this.fileList.find(
  128. (file) => file.name === item.originFileName
  129. );
  130. fileData.fileName = item.originFileName;
  131. fileData.url = item.fileUrl;
  132. this.uploadFileList.push(fileData);
  133. });
  134. this.$emit("input", this.uploadFileList);
  135. this.$message.success("上传成功");
  136. } else {
  137. this.$message.error("上传失败");
  138. }
  139. });
  140. });
  141. },
  142. // 上传
  143. submitUpload() {
  144. this.$refs.upload.submit();
  145. // // 判断是否有文件
  146. // if (this.fileList.length === 0) {
  147. // return this.$message.warning("请选取文件后再上传");
  148. // }
  149. // // 判断是否有重复文件
  150. // let arr1 = this.fileList.map((i) => i.name);
  151. // let arr2 = [...new Set(arr1)];
  152. // if (arr1.length !== arr2.length) {
  153. // return this.$message.warning("请去掉重复文件后再上传");
  154. // }
  155. // // 开始上传
  156. // const formData = new FormData();
  157. // this.fileList.forEach((file) => {
  158. // formData.append("file", file.raw);
  159. // });
  160. // uploadFiles(formData).then(({ data }) => {
  161. // if (data && data.code === "200") {
  162. // data.data.forEach((item) => {
  163. // let fileData = this.fileList.find(
  164. // (file) => file.name === item.originFileName
  165. // );
  166. // fileData.url = item.fileUrl;
  167. // });
  168. // this.$message.success("上传成功");
  169. // } else {
  170. // this.$message.error("上传失败");
  171. // }
  172. // });
  173. },
  174. // 移除
  175. handleRemove(file, fileList) {
  176. this.fileList = fileList;
  177. },
  178. // 预览
  179. handlePreview(file) {
  180. if (!file || !file.name || !file.url) {
  181. this.$message.error("没有可预览的文件!");
  182. return;
  183. }
  184. let ext = getFileExt(file.name);
  185. if (ext === "jpg" || ext === "jpeg" || ext === "png" || ext === "gif") {
  186. this.previewPath = downloadUrl + file.url;
  187. this.previewName = file.name;
  188. this.previewVisible = true;
  189. } else {
  190. // 弹出新页面显示下载文件
  191. window.open(downloadUrl + file.url, "_blank");
  192. }
  193. },
  194. // 改变上传内容
  195. handleChange(file, fileList) {
  196. this.fileList = fileList;
  197. },
  198. // 超限
  199. handleExceed(files, fileList) {
  200. this.$message.warning(
  201. `当前限制选择 ${this.limit} 个文件,本次选择了 ${
  202. files.length
  203. } 个文件,共选择了 ${files.length + fileList.length} 个文件`
  204. );
  205. },
  206. },
  207. };
  208. </script>
  209. <style scoped></style>