upload-component.vue 5.8 KB

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