StdMultiFilesUpload.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div>
  3. <a-upload
  4. :before-upload="beforeUpload"
  5. :multiple="true"
  6. :show-upload-list="true"
  7. :file-list="uploadList"
  8. :remove="remove"
  9. >
  10. <a-button :disabled="disabled"><a-icon type="upload"/>选择文件</a-button>
  11. </a-upload>
  12. <a-button
  13. type="primary"
  14. :disabled="uploadList.length === 0 && !id"
  15. :loading="uploading"
  16. style="margin: 16px 0"
  17. @click="upload"
  18. v-if="id"
  19. >
  20. {{ uploading ? '上传中' : '开始上传' }}
  21. </a-button>
  22. <p style="margin: 15px 0" v-for="file in uploaded" :key="file.id">
  23. <a-icon type="paper-clip" style="margin-right: 5px"/>
  24. <a :href="server + '/' + file.path" target="_blank" @click="()=>{}">{{ getFileName(file.path) }}</a>
  25. <a-popconfirm
  26. title="确定要删除文件吗"
  27. ok-text="确认"
  28. cancel-text="取消"
  29. @confirm="deleteFile(file.id)"
  30. style="float: right"
  31. >
  32. <a-button type="link">删除</a-button>
  33. </a-popconfirm>
  34. </p>
  35. </div>
  36. </template>
  37. <script>
  38. export default {
  39. name: "StdMultiFilesUpload",
  40. props: {
  41. api: Function,
  42. id: {
  43. type: Number,
  44. default: null
  45. },
  46. fileList: {
  47. default: null
  48. },
  49. autoUpload: {
  50. type: Boolean,
  51. default: false
  52. },
  53. api_delete: {
  54. type: Function,
  55. default: null
  56. },
  57. disabled: {
  58. type: Boolean,
  59. default: false
  60. }
  61. },
  62. watch: {
  63. fileList() {
  64. this.uploaded = this.fileList
  65. }
  66. },
  67. data() {
  68. return {
  69. uploadList: [],
  70. uploaded: this.fileList,
  71. lastFileTime: 0,
  72. server: process.env["VUE_APP_API_UPLOAD_ROOT"],
  73. uploading: false,
  74. }
  75. },
  76. model: {
  77. prop: 'fileUrl',
  78. event: 'changeFileUrl'
  79. },
  80. methods: {
  81. async upload() {
  82. if (this.uploadList.length) {
  83. this.uploading = true
  84. let formData = new FormData()
  85. while (this.uploadList.length) {
  86. formData.append('file[]', this.uploadList.shift())
  87. }
  88. this.visible = false
  89. this.uploading = true
  90. this.$message.info('正在上传附件, 请不要关闭本页')
  91. return this.api(this.id, formData).then(r => {
  92. this.uploaded = [...this.uploaded, ...r]
  93. this.uploading = false
  94. this.$emit('uploaded', r)
  95. this.uploading = false
  96. this.orig = false
  97. this.$message.success('上传成功')
  98. }).catch(e => {
  99. this.$message.error(e.message ? e.message : '上传失败')
  100. })
  101. }
  102. },
  103. beforeUpload(file) {
  104. this.uploadList.push(file)
  105. return false
  106. },
  107. deleteFile(file_id) {
  108. this.api_delete(this.id, file_id).then(r => {
  109. this.uploaded = r
  110. })
  111. },
  112. getFileName(path) {
  113. // 从15开始找
  114. const idx = path.indexOf("/", 15)
  115. return path.substring(idx + 1)
  116. },
  117. remove(r) {
  118. this.uploadList = this.uploadList.filter(value => {
  119. return value !== r
  120. })
  121. },
  122. }
  123. }
  124. </script>
  125. <style scoped>
  126. </style>