uploadFile.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <div>
  3. <el-upload
  4. ref="upload"
  5. class="upload-demo"
  6. :limit="1"
  7. :data="params"
  8. :action="action"
  9. :file-list="fileList"
  10. :auto-upload="false"
  11. :headers="headers"
  12. :on-success="success"
  13. :accept="accept"
  14. :before-upload="beforeUpload"
  15. :on-remove="remove"
  16. :on-change="change"
  17. >
  18. <el-button slot="trigger" size="mini" type="primary">选取文件</el-button>
  19. <!-- <el-button style="margin-left: 10px;" size="mini" type="success" @click="uploadFile">上传</el-button> -->
  20. <div slot="tip" class="el-upload__tip">只能上传.doc/.ppt/.pdf文件,建议大小在10M内</div>
  21. </el-upload>
  22. </div>
  23. </template>
  24. <script>
  25. import { mapGetters } from 'vuex'
  26. export default {
  27. props: {
  28. fileList: {
  29. type: Array,
  30. default: () => []
  31. },
  32. authUserId: {
  33. type: Number,
  34. default: -1
  35. },
  36. brandId: {
  37. type: [Number, String],
  38. default: 0
  39. }
  40. },
  41. data() {
  42. return {
  43. // 文件上传接口
  44. action: process.env.VUE_APP_UPLOAD_API + '/shop/upload/file',
  45. accept: '.doc,.ppt,.pdf',
  46. // 请求参数
  47. // params: {
  48. // authUserId: null,
  49. // brandId: this.brandId || 0
  50. // },
  51. headers: {
  52. 'X-Token': ''
  53. },
  54. showUpload: true
  55. }
  56. },
  57. computed: {
  58. ...mapGetters(['token']),
  59. params() {
  60. return {
  61. authUserId: null,
  62. brandId: this.brandId || 0
  63. }
  64. }
  65. },
  66. created() {
  67. this.headers['X-Token'] = this.token
  68. },
  69. methods: {
  70. success(response) {
  71. this.$emit('success', response)
  72. },
  73. error(err, file, fileList) {
  74. this.$emit('error', err, file, fileList)
  75. },
  76. uploadFile() {
  77. this.params.authUserId = this.authUserId || -1
  78. this.$refs.upload.submit()
  79. },
  80. // 文件上传之前的钩子
  81. beforeUpload(file) {
  82. this.params.authUserId = this.authUserId || -1
  83. const size = file.size
  84. if (size > 1024 * 5000) {
  85. const h = this.$createElement
  86. this.$notify.info({
  87. title: '上传文件过大',
  88. message: h('p', { style: 'color: #333' }, '文件上传保存需要一定的时间,请您耐心等待,在此期间请勿执执行其他操作,以免影响文件上传失败!'),
  89. duration: 5000
  90. })
  91. }
  92. },
  93. remove() {
  94. this.$emit('remove')
  95. },
  96. change() {
  97. this.$emit('change')
  98. }
  99. }
  100. }
  101. </script>