uploadFile.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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文件,且不超过2MB</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: null
  49. },
  50. headers: {
  51. 'X-Token': ''
  52. },
  53. showUpload: true
  54. }
  55. },
  56. computed: {
  57. ...mapGetters(['token'])
  58. },
  59. created() {
  60. this.headers['X-Token'] = this.token
  61. this.params.brandId = this.brandId || 0
  62. },
  63. methods: {
  64. success(response) {
  65. this.$emit('success', response)
  66. },
  67. error(err, file, fileList) {
  68. this.$emit('error', err, file, fileList)
  69. },
  70. uploadFile() {
  71. this.params.authUserId = this.authUserId || -1
  72. this.$refs.upload.submit()
  73. },
  74. // 图片上传之前的钩子
  75. beforeUpload(file) {
  76. this.params.authUserId = this.authUserId || -1
  77. const size = file.size
  78. if (size > 1024 * 2000) {
  79. this.$message.warning('文件上传大小超出限制(≤2MB)')
  80. return false
  81. }
  82. },
  83. remove() {
  84. this.$emit('remove')
  85. }
  86. }
  87. }
  88. </script>