uploadFile.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <div class="upAppendix">
  3. <el-upload
  4. action="string"
  5. class="upload-demo"
  6. :on-remove="handleRemove"
  7. :before-remove="beforeRemove"
  8. :http-request="upFile"
  9. :multiple="false"
  10. :file-list="nFile"
  11. :accept="acceptType"
  12. >
  13. <div class="add-wrap" v-if="!showBtn">
  14. <p class="add">
  15. <i class="el-icon-upload2"></i>点击选择
  16. </p>
  17. </div>
  18. </el-upload>
  19. </div>
  20. </template>
  21. <script type="text/ecmascript-6">
  22. export default {
  23. name: 'singleImg',
  24. props: {
  25. file: {
  26. type: Object,
  27. default: () => {
  28. return {};
  29. }
  30. },
  31. fileType: {
  32. type: String,
  33. default: '3'
  34. },
  35. acceptType: {
  36. type: String,
  37. // default: 'audio/*'
  38. default: '.m4a,audio/wav,audio/mp3,.aac'
  39. }
  40. },
  41. data() {
  42. return {
  43. showBtn: false,
  44. uploadPercent: '', // 上传进度
  45. currentFile: {},
  46. defaultImg: this.$Public.defaultImg,
  47. nFile: [],
  48. progress: 0 // 进度条
  49. };
  50. },
  51. created() {},
  52. methods: {
  53. beforeRemove(file) {
  54. console.log('beforeRemove', file);
  55. return this.$confirm(`确定移除 ${file.name}?`);
  56. },
  57. handleRemove(file) {
  58. this.showBtn = false;
  59. this.nFile = this.nFile.filter(item => item !== file);
  60. this.emit();
  61. },
  62. emit() {
  63. let data = {
  64. file_url: this.nFile[0].url,
  65. url_name: this.nFile[0].name
  66. };
  67. this.$emit('getFile', data);
  68. },
  69. upFile(param) {
  70. // 上传
  71. let file = param.file;
  72. if (!file) {
  73. return;
  74. }
  75. this.showBtn = true;
  76. this.httpUploadImg({ type: this.fileType, file: file }).then(
  77. res => {
  78. let item = {
  79. url: res,
  80. name: param.file.name
  81. };
  82. this.nFile.push(item);
  83. console.log('httpUploadImg', this.nFile, res);
  84. this.emit();
  85. },
  86. res => {
  87. console.log(2222);
  88. }
  89. );
  90. }
  91. },
  92. watch: {
  93. file: {
  94. handler(val) {
  95. console.log(22222, val);
  96. // if (val.id) {
  97. if (val.file_url) {
  98. this.nFile = [
  99. {
  100. url: val.file_url,
  101. name: val.url_name
  102. }
  103. ];
  104. this.showBtn = true;
  105. }
  106. // }
  107. },
  108. deep: true,
  109. immediate: true
  110. },
  111. nFile: {
  112. handler(val) {
  113. console.log(333, val);
  114. },
  115. deep: true,
  116. immediate: true
  117. }
  118. }
  119. };
  120. </script>
  121. <style lang="stylus" rel="stylesheet/stylus" scoped>
  122. @import '~assets/main.styl';
  123. .upAppendix {
  124. flex-x(flex-start, flex-start);
  125. margin-top: 5px;
  126. .add-wrap {
  127. position: relative;
  128. width: 100px;
  129. height: 30px;
  130. background: white;
  131. border: 1px solid border-color;
  132. border-radius(3px);
  133. flex-center();
  134. .add {
  135. color: blue;
  136. }
  137. p {
  138. text-align: center;
  139. color: gray6;
  140. }
  141. }
  142. input {
  143. width: 100%;
  144. height: 100%;
  145. opacity: 0;
  146. position: absolute;
  147. top: 0;
  148. left: 0;
  149. }
  150. }
  151. >>>.el-upload {
  152. display: inherit !important;
  153. }
  154. >>>.el-upload-list__item:first-child {
  155. margin-top: 0 !important;
  156. }
  157. </style>