index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <script>
  2. import UploadList from './upload-list';
  3. import Upload from './upload';
  4. import IframeUpload from './iframe-upload';
  5. import ElProgress from 'element-ui/packages/progress';
  6. function noop() {
  7. }
  8. export default {
  9. name: 'el-upload',
  10. components: {
  11. ElProgress,
  12. UploadList,
  13. Upload,
  14. IframeUpload
  15. },
  16. props: {
  17. action: {
  18. type: String,
  19. required: true
  20. },
  21. headers: {
  22. type: Object,
  23. default() {
  24. return {};
  25. }
  26. },
  27. data: Object,
  28. multiple: Boolean,
  29. name: {
  30. type: String,
  31. default: 'file'
  32. },
  33. withCredentials: Boolean,
  34. thumbnailMode: Boolean,
  35. showUploadList: {
  36. type: Boolean,
  37. default: true
  38. },
  39. accept: String,
  40. type: {
  41. type: String,
  42. default: 'select'
  43. },
  44. beforeUpload: Function,
  45. onRemove: {
  46. type: Function,
  47. default: noop
  48. },
  49. onChange: {
  50. type: Function,
  51. default: noop
  52. },
  53. onPreview: {
  54. type: Function,
  55. default: noop
  56. },
  57. onSuccess: {
  58. type: Function,
  59. default: noop
  60. },
  61. onError: {
  62. type: Function,
  63. default: noop
  64. },
  65. defaultFileList: {
  66. type: Array,
  67. default() {
  68. return [];
  69. }
  70. }
  71. },
  72. data() {
  73. return {
  74. fileList: [],
  75. dragOver: false,
  76. draging: false,
  77. tempIndex: 1
  78. };
  79. },
  80. methods: {
  81. handleStart(file) {
  82. file.uid = Date.now() + this.tempIndex++;
  83. let _file = {
  84. status: 'uploading',
  85. name: file.name,
  86. size: file.size,
  87. percentage: 0,
  88. uid: file.uid,
  89. showProgress: true
  90. };
  91. if (this.thumbnailMode) {
  92. try {
  93. _file.url = URL.createObjectURL(file);
  94. } catch (err) {
  95. console.log(err);
  96. return;
  97. }
  98. }
  99. this.fileList.push(_file);
  100. },
  101. handleProgress(ev, file) {
  102. var _file = this.getFile(file);
  103. _file.percentage = ev.percent || 0;
  104. },
  105. handleSuccess(res, file) {
  106. var _file = this.getFile(file);
  107. if (_file) {
  108. _file.status = 'finished';
  109. _file.response = res;
  110. this.onSuccess(res, _file, this.fileList);
  111. setTimeout(() => {
  112. _file.showProgress = false;
  113. }, 1000);
  114. }
  115. },
  116. handleError(err, response, file) {
  117. var _file = this.getFile(file);
  118. var fileList = this.fileList;
  119. _file.status = 'fail';
  120. fileList.splice(fileList.indexOf(_file), 1);
  121. this.onError(err, response, file);
  122. },
  123. handleRemove(file) {
  124. var fileList = this.fileList;
  125. fileList.splice(fileList.indexOf(file), 1);
  126. this.onRemove(file, fileList);
  127. },
  128. getFile(file) {
  129. var fileList = this.fileList;
  130. var target;
  131. fileList.every(item => {
  132. target = file.uid === item.uid ? item : null;
  133. return !target;
  134. });
  135. return target;
  136. },
  137. handlePreview(file) {
  138. if (file.status === 'finished') {
  139. this.onPreview(file);
  140. }
  141. },
  142. clearFiles() {
  143. this.fileList = [];
  144. }
  145. },
  146. watch: {
  147. defaultFileList: {
  148. immediate: true,
  149. handler(fileList) {
  150. this.fileList = fileList.map(item => {
  151. item.status = 'finished';
  152. item.percentage = 100;
  153. item.uid = Date.now() + this.tempIndex++;
  154. return item;
  155. });
  156. }
  157. }
  158. },
  159. render(h) {
  160. var uploadList;
  161. if (this.showUploadList && !this.thumbnailMode && this.fileList.length) {
  162. uploadList = (
  163. <UploadList
  164. files={this.fileList}
  165. on-remove={this.handleRemove}
  166. on-preview={this.handlePreview}>
  167. </UploadList>
  168. );
  169. }
  170. var props = {
  171. props: {
  172. type: this.type,
  173. action: this.action,
  174. multiple: this.multiple,
  175. 'before-upload': this.beforeUpload,
  176. 'with-credentials': this.withCredentials,
  177. headers: this.headers,
  178. name: this.name,
  179. data: this.data,
  180. accept: this.thumbnailMode ? 'image/*' : this.accept,
  181. 'on-start': this.handleStart,
  182. 'on-progress': this.handleProgress,
  183. 'on-success': this.handleSuccess,
  184. 'on-error': this.handleError,
  185. 'on-preview': this.handlePreview,
  186. 'on-remove': this.handleRemove
  187. },
  188. ref: 'upload-inner'
  189. };
  190. var uploadComponent = typeof FormData !== 'undefined'
  191. ? <upload {...props}>{this.$slots.default}</upload>
  192. : <iframeUpload {...props}>{this.$slots.default}</iframeUpload>;
  193. if (this.type === 'select') {
  194. return (
  195. <div class="el-upload">
  196. {uploadList}
  197. {uploadComponent}
  198. {this.$slots.tip}
  199. </div>
  200. );
  201. }
  202. if (this.type === 'drag') {
  203. return (
  204. <div class="el-upload">
  205. {uploadComponent}
  206. {this.$slots.tip}
  207. {uploadList}
  208. </div>
  209. );
  210. }
  211. }
  212. };
  213. </script>