uploadFile.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. >
  17. <el-button slot="trigger" size="mini" type="primary">选取文件</el-button>
  18. <!-- <el-button style="margin-left: 10px;" size="mini" type="success" @click="uploadFile">上传</el-button> -->
  19. <div slot="tip" class="el-upload__tip">只能上传.doc/.ppt/.pdf文件,建议大小在10M内</div>
  20. </el-upload>
  21. </div>
  22. </template>
  23. <script>
  24. import { mapGetters } from 'vuex'
  25. export default {
  26. props: {
  27. fileList: {
  28. type: Array,
  29. default: () => []
  30. },
  31. authUserId: {
  32. type: Number,
  33. default: -1
  34. },
  35. brandId: {
  36. type: [Number, String],
  37. default: 0
  38. }
  39. },
  40. data() {
  41. return {
  42. // 文件上传接口
  43. action: process.env.VUE_APP_UPLOAD_API + '/shop/upload/file',
  44. accept: '.doc,.ppt,.pdf',
  45. // 请求参数
  46. // params: {
  47. // authUserId: null,
  48. // brandId: this.brandId || 0
  49. // },
  50. headers: {
  51. 'X-Token': ''
  52. },
  53. showUpload: true
  54. }
  55. },
  56. computed: {
  57. ...mapGetters(['token']),
  58. params() {
  59. return {
  60. authUserId: null,
  61. brandId: this.brandId || 0
  62. }
  63. }
  64. },
  65. created() {
  66. this.headers['X-Token'] = this.token
  67. },
  68. methods: {
  69. success(response) {
  70. this.$emit('success', response)
  71. },
  72. error(err, file, fileList) {
  73. this.$emit('error', err, file, fileList)
  74. },
  75. uploadFile() {
  76. this.params.authUserId = this.authUserId || -1
  77. this.$refs.upload.submit()
  78. },
  79. // 文件上传之前的钩子
  80. beforeUpload(file) {
  81. this.params.authUserId = this.authUserId || -1
  82. // const size = file.size
  83. // if (size > 1024 * 2000) {
  84. // this.$message.warning('文件上传大小超出限制(≤2MB)')
  85. // return false
  86. // }
  87. },
  88. remove() {
  89. this.$emit('remove')
  90. }
  91. }
  92. }
  93. </script>