upload.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <div class="el-upload__inner"
  3. :class="{
  4. 'el-dragger': type === 'drag',
  5. 'is-dragOver': dragOver,
  6. 'is-showCover': showCover
  7. }"
  8. @click="handleClick"
  9. @drop.prevent="onDrop"
  10. @dragover.prevent="dragOver = true"
  11. @dragleave.prevent="dragOver = false">
  12. <slot v-if="!showCover"></slot>
  13. <cover :image="lastestFile" :on-preview="onPreview" :on-remove="onRemove" v-else></cover>
  14. <input class="el-upload__input" type="file" ref="input" @change="handleChange" :multiple="multiple" :accept="accept">
  15. </div>
  16. </template>
  17. <script>
  18. import ajax from './ajax';
  19. import Cover from './cover';
  20. export default {
  21. components: {
  22. Cover
  23. },
  24. props: {
  25. type: String,
  26. action: {
  27. type: String,
  28. required: true
  29. },
  30. name: {
  31. type: String,
  32. default: 'file'
  33. },
  34. data: Object,
  35. headers: Object,
  36. withCredentials: Boolean,
  37. multiple: Boolean,
  38. accept: String,
  39. onStart: Function,
  40. onProgress: Function,
  41. onSuccess: Function,
  42. onError: Function,
  43. beforeUpload: Function,
  44. onPreview: {
  45. type: Function,
  46. default: function() {}
  47. },
  48. onRemove: {
  49. type: Function,
  50. default: function() {}
  51. }
  52. },
  53. data() {
  54. return {
  55. dragOver: false,
  56. mouseover: false
  57. };
  58. },
  59. computed: {
  60. lastestFile() {
  61. var fileList = this.$parent.fileList;
  62. return fileList[fileList.length - 1];
  63. },
  64. showCover() {
  65. var file = this.lastestFile;
  66. return this.thumbnailMode && file && file.status !== 'fail';
  67. },
  68. thumbnailMode() {
  69. return this.$parent.thumbnailMode;
  70. }
  71. },
  72. methods: {
  73. isImage(str) {
  74. return str.indexOf('image') !== -1;
  75. },
  76. handleChange(ev) {
  77. const files = ev.target.files;
  78. if (!files) {
  79. return;
  80. }
  81. this.uploadFiles(files);
  82. this.$refs.input.value = null;
  83. },
  84. uploadFiles(files) {
  85. let postFiles = Array.prototype.slice.call(files);
  86. if (!this.multiple) { postFiles = postFiles.slice(0, 1); }
  87. if (postFiles.length === 0) { return; }
  88. postFiles.forEach(file => {
  89. let isImage = this.isImage(file.type);
  90. if (this.thumbnailMode && !isImage) {
  91. return;
  92. } else {
  93. this.upload(file);
  94. }
  95. });
  96. },
  97. upload(file) {
  98. if (!this.beforeUpload) {
  99. return this.post(file);
  100. }
  101. const before = this.beforeUpload(file);
  102. if (before && before.then) {
  103. before.then(processedFile => {
  104. if (Object.prototype.toString.call(processedFile) === '[object File]') {
  105. this.post(processedFile);
  106. } else {
  107. this.post(file);
  108. }
  109. }, () => {
  110. // this.$emit('cancel', file);
  111. });
  112. } else if (before !== false) {
  113. this.post(file);
  114. } else {
  115. // this.$emit('cancel', file);
  116. }
  117. },
  118. post(file) {
  119. this.onStart(file);
  120. let formData = new FormData();
  121. formData.append(this.name, file);
  122. ajax(this.action, {
  123. headers: this.headers,
  124. withCredentials: this.withCredentials,
  125. file: file,
  126. data: this.data,
  127. filename: this.name,
  128. onProgress: e => {
  129. this.onProgress(e, file);
  130. },
  131. onSuccess: res => {
  132. this.onSuccess(res, file);
  133. },
  134. onError: err => {
  135. this.onError(err, file);
  136. }
  137. });
  138. },
  139. onDrop(e) {
  140. this.dragOver = false;
  141. this.uploadFiles(e.dataTransfer.files);
  142. },
  143. handleClick() {
  144. this.$refs.input.click();
  145. }
  146. }
  147. };
  148. </script>