uploadFile.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <script>
  2. const acceptMap = {
  3. 0: '*/*',
  4. 1: 'image/*',
  5. 2: 'video/*',
  6. 3: 'audio/*',
  7. // svg
  8. 4: 'image/svg+xml',
  9. };
  10. export default {
  11. name: 'uploadFile',
  12. props: {
  13. // 多选
  14. multiple: {
  15. type: Boolean,
  16. default: false
  17. },
  18. // 类型限制 0: all 1: image 2: video 3: audio
  19. type: {
  20. type: Number,
  21. default: 0
  22. },
  23. customAccept : {
  24. type: String,
  25. default: ''
  26. },
  27. // 是否预览选择的资源
  28. preview: {
  29. type: Boolean,
  30. default: false
  31. },
  32. },
  33. data(){
  34. return {
  35. }
  36. },
  37. computed:{
  38. acceptComputed(){
  39. if(this.customAccept){
  40. return this.customAccept;
  41. }
  42. return acceptMap[this.type]?acceptMap[this.type]:acceptMap["0"];
  43. },
  44. },
  45. methods: {
  46. changeHandle(e) {
  47. console.log(e)
  48. console.log(e.target.files)
  49. let files = e.target.files;
  50. if(files.length === 0){
  51. return;
  52. }
  53. this.$emit('change',files);
  54. }
  55. }
  56. }
  57. </script>
  58. <template>
  59. <div class="uploadFile">
  60. <div class="showTips">
  61. <slot>
  62. <div class="showTips-con-text">点击上传</div>
  63. </slot>
  64. </div>
  65. <input type="file"
  66. :multiple="multiple"
  67. :accept="acceptComputed"
  68. @change="changeHandle"
  69. />
  70. </div>
  71. </template>
  72. <style scoped>
  73. .uploadFile{
  74. width: 100%;
  75. height: 100%;
  76. position: relative;
  77. }
  78. .uploadFile input{
  79. width: 100%;
  80. height: 100%;
  81. position: absolute;
  82. left: 0;
  83. top: 0;
  84. opacity: 0;
  85. }
  86. .uploadFile .showTips{
  87. width: 100%;
  88. height: 100%;
  89. position: absolute;
  90. left: 0;
  91. top: 0;
  92. cursor: pointer;
  93. }
  94. .showTips .showTips-con-text{
  95. color: #fff;
  96. font-size: 24px;
  97. font-weight: bold;
  98. background-color: #6c6c6c;
  99. width: 100%;
  100. height: 100%;
  101. display: flex;
  102. justify-content: center;
  103. align-items: center;
  104. }
  105. </style>