uploadFile.vue 1.8 KB

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