upload-component-v2.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <section>
  3. <el-col :span="24">
  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. if (newVal && newVal instanceof Array) {
  97. newVal.forEach(t => { t.name = t.fileName })
  98. }
  99. this.fileList = newVal
  100. },
  101. value (newVal) {
  102. if (newVal && newVal instanceof Array) {
  103. newVal.forEach(t => { t.name = t.fileName })
  104. this.fileList = this.uploadFileList = newVal
  105. }
  106. }
  107. },
  108. data () {
  109. return {
  110. fileList: [],
  111. uploadUrl: uploadUrl,
  112. previewPath: '',
  113. previewName: '',
  114. previewVisible: false,
  115. uploadFileList: [] // 已经上传过的文件列表
  116. }
  117. },
  118. methods: {
  119. handleUpload (file) {
  120. function getBase64 (file) {
  121. return new Promise((resolve, reject) => {
  122. const reader = new FileReader()
  123. reader.readAsDataURL(file)
  124. reader.onload = () => resolve(reader.result)
  125. reader.onerror = (error) => reject(error)
  126. })
  127. }
  128. return getBase64(file.file).then((res) => {
  129. // 需要return才会显示上传成功状态,不加return不好使
  130. // 开始上传
  131. const formData = new FormData()
  132. formData.append('file', file.file)
  133. uploadFiles(formData).then(({ data }) => {
  134. if (data && data.code === '200') {
  135. data.data.forEach((item) => {
  136. let fileData = this.fileList.find(
  137. (file) => file.name === item.originFileName
  138. )
  139. fileData.fileName = item.originFileName
  140. fileData.url = item.fileUrl
  141. this.uploadFileList.push(fileData)
  142. })
  143. this.$emit('input', this.uploadFileList)
  144. this.$message.success('上传成功')
  145. } else {
  146. this.$message.error('上传失败')
  147. }
  148. })
  149. })
  150. },
  151. // 上传
  152. submitUpload () {
  153. this.$refs.upload.submit()
  154. },
  155. // 移除
  156. handleRemove (file, fileList) {
  157. this.$emit('input', fileList)
  158. },
  159. // 预览
  160. handlePreview (file) {
  161. if (!file || !file.name || !file.url) {
  162. this.$message.error('没有可预览的文件!')
  163. return
  164. }
  165. let ext = getFileExt(file.name)
  166. if (ext === 'jpg' || ext === 'jpeg' || ext === 'png' || ext === 'gif') {
  167. this.previewPath = downloadUrl + file.url
  168. this.previewName = file.name
  169. this.previewVisible = true
  170. } else {
  171. // 弹出新页面显示下载文件
  172. window.open(downloadUrl + file.url, '_blank')
  173. }
  174. },
  175. // 改变上传内容
  176. handleChange (file, fileList) {
  177. this.fileList = fileList
  178. },
  179. // 超限
  180. handleExceed (files, fileList) {
  181. this.$message.warning(
  182. `当前限制选择 ${this.limit} 个文件,本次选择了 ${
  183. files.length
  184. } 个文件,共选择了 ${files.length + fileList.length} 个文件`
  185. )
  186. }
  187. }
  188. }
  189. </script>
  190. <style scoped></style>