index.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div class="upload">
  3. <div class="upload-box" @click="choiceFile" v-loading="loading">
  4. <div class="upload-btn" v-if="src == ''">
  5. <i class="el-icon-upload"></i>
  6. <span class="upload-text">点击选择文件</span>
  7. <span><small>允许上传格式:jpg/png/gif,文件大小不超过5M</small></span>
  8. </div>
  9. <div class="upload-show" v-else>
  10. <img :src="src" alt="" />
  11. </div>
  12. </div>
  13. <input type="file" name="file" ref="file" :multiple="multiple" @change="getFile" style="display: none" :accept="accept" />
  14. </div>
  15. </template>
  16. <script>
  17. import upload from '@/utils/oss.js'
  18. export default {
  19. data() {
  20. return {
  21. url: '',
  22. src: '',
  23. multiple: false,
  24. loading: false,
  25. accept: 'image/jpg,image/jpeg,image/png,image/gif'
  26. }
  27. },
  28. methods: {
  29. choiceFile() {
  30. //this.loading = true
  31. this.$refs.file.click()
  32. },
  33. getFile(e) {
  34. //this.loading = false
  35. let file = e.target.files[0]
  36. this.src = URL.createObjectURL(file)
  37. this.$emit('getFileInfo', file)
  38. },
  39. sub() {
  40. upload(this.files).then((res) => {
  41. console.log(res)
  42. })
  43. }
  44. }
  45. }
  46. </script>
  47. <style scoped lang="scss">
  48. .upload-box {
  49. width: 360px;
  50. height: 300px;
  51. user-select: none;
  52. -webkit-user-select: none;
  53. cursor: pointer;
  54. .upload-btn {
  55. width: 100%;
  56. height: 100%;
  57. border: 1px dashed #d9d9d9;
  58. display: flex;
  59. flex-direction: column;
  60. align-items: center;
  61. justify-content: center;
  62. color: #666;
  63. .upload-text {
  64. display: inline-block;
  65. margin: 10px 0;
  66. }
  67. i {
  68. font-size: 50px;
  69. color: #c0c4cc;
  70. }
  71. }
  72. .upload-show {
  73. width: 100%;
  74. height: 100%;
  75. border: 1px solid #d9d9d9;
  76. padding: 10px;
  77. box-sizing: border-box;
  78. img {
  79. width: 100%;
  80. height: 100%;
  81. }
  82. }
  83. }
  84. </style>