UploadSpareFile.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <div class="clearfix">
  3. <a-upload
  4. :action="action"
  5. listType="picture-card"
  6. :fileList="fileList"
  7. @change="handleChange"
  8. :customRequest="customRequest"
  9. :remove="handleRemove"
  10. :beforeUpload="beforeUpload"
  11. >
  12. <div v-if="fileList.length < maxSize">
  13. <a-icon type="plus" />
  14. <div class="ant-upload-text">Upload</div>
  15. </div>
  16. </a-upload>
  17. <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel" :width="1200">
  18. <img style="width: 100%" :src="previewImage" />
  19. </a-modal>
  20. </div>
  21. </template>
  22. <script>
  23. import { uploadFileSpare } from '@/api/upms/file'
  24. export default {
  25. name: 'UploadSpareImg',
  26. data () {
  27. return {
  28. previewVisible: false,
  29. action: '/upms/files/upload/spare',
  30. maxSize: 1,
  31. previewImage: '',
  32. fileList: []
  33. }
  34. },
  35. methods: {
  36. base (maxSize, fileList) {
  37. this.fileList = fileList
  38. this.maxSize = maxSize
  39. },
  40. handleCancel () {
  41. this.previewVisible = false
  42. },
  43. handlePreview (file) {
  44. this.previewImage = file.url || file.thumbUrl
  45. this.previewVisible = true
  46. },
  47. handleChange ({ fileList }) {
  48. // this.fileList = fileList
  49. // this.$emit('catchImage', fileList)
  50. },
  51. async customRequest (data) {
  52. const formData = new FormData()
  53. formData.append('file', data.file)
  54. data.onProgress()
  55. const that = this
  56. uploadFileSpare(formData).then(res => {
  57. data.onSuccess()
  58. that.fileList.push({
  59. uid: '-1',
  60. name: res.data.fileName,
  61. status: 'done',
  62. url: this.BaseTool.Constant.FILE_URL + res.data.url
  63. })
  64. this.$emit('catchImage', that.fileList)
  65. })
  66. },
  67. handleRemove (file) {
  68. const index = this.fileList.indexOf(file)
  69. const newFileList = this.fileList.slice()
  70. newFileList.splice(index, 1)
  71. this.fileList = newFileList
  72. this.$emit('catchImage', this.fileList)
  73. return true
  74. },
  75. beforeUpload (file, fileList) {
  76. }
  77. }
  78. }
  79. </script>
  80. <style>
  81. /* you can make up upload button and sample style by using stylesheets */
  82. .ant-upload-select-picture-card i {
  83. font-size: 32px;
  84. color: #999;
  85. }
  86. .ant-upload-select-picture-card .ant-upload-text {
  87. margin-top: 8px;
  88. color: #666;
  89. }
  90. .ant-upload-btn {
  91. margin-top: 20px;
  92. }
  93. </style>