MyUpload.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <div>
  3. <el-upload
  4. v-bind="$props"
  5. :http-request="onUpload">
  6. <slot></slot>
  7. </el-upload>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: "MyUpload",
  13. props: {
  14. accept: String,
  15. action: {
  16. type: String,
  17. required: true
  18. },
  19. autoUpload: {
  20. type: Boolean,
  21. default: true
  22. },
  23. beforeRemove:Function,
  24. beforeUpload: Function,
  25. disabled: Boolean,
  26. drag: Boolean,
  27. dragger: Boolean,
  28. fileList: Array,
  29. headers: Object,
  30. httpRequest: Function,
  31. isBlob:Boolean,
  32. limit: Number,
  33. listType: String,
  34. multiple: Boolean,
  35. name: String,
  36. onChange: Function,
  37. onError: Function,
  38. onExceed: Function,
  39. onPreview: Function,
  40. onProgress: Function,
  41. onRemove: Function,
  42. onSuccess: Function,
  43. showFileList: Boolean,
  44. type: String,
  45. withCredentials: Boolean
  46. },
  47. data() {
  48. return {
  49. };
  50. },
  51. mounted() {
  52. },
  53. methods: {
  54. onUpload(option) {
  55. var _this = this;
  56. if (this.httpRequest) {
  57. return this.httpRequest();
  58. }
  59. option.isBlob = this.isBlob;
  60. if (typeof XMLHttpRequest === 'undefined') {
  61. return;
  62. }
  63. var xhr = new XMLHttpRequest();
  64. var action = option.action;
  65. if (xhr.upload) {
  66. xhr.upload.onprogress = function progress(e) {
  67. if (e.total > 0) {
  68. e.percent = e.loaded / e.total * 100;
  69. }
  70. option.onProgress(e);
  71. };
  72. }
  73. var formData = new FormData();
  74. if (option.data) {
  75. Object.keys(option.data).forEach(function (key) {
  76. formData.append(key, option.data[key]);
  77. });
  78. }
  79. formData.append(option.filename, option.file, option.file.name);
  80. xhr.onerror = function error(e) {
  81. option.onError(e);
  82. };
  83. xhr.onload = function() {
  84. if (xhr.status < 200 || xhr.status >= 300) {
  85. return option.onError(getError(action, option, xhr));
  86. }
  87. if (option.isBlob) {
  88. option.onSuccess(xhr.response);
  89. } else {
  90. option.onSuccess(_this.getBody(xhr));
  91. }
  92. };
  93. if (option.isBlob) {
  94. xhr.responseType="blob";
  95. }
  96. xhr.open('post', action, true);
  97. if (option.withCredentials && 'withCredentials' in xhr) {
  98. xhr.withCredentials = true;
  99. }
  100. var headers = option.headers || {};
  101. for (var item in headers) {
  102. if (headers.hasOwnProperty(item) && headers[item] !== null) {
  103. xhr.setRequestHeader(item, headers[item]);
  104. }
  105. }
  106. xhr.send(formData);
  107. return xhr;
  108. },
  109. getBody(xhr) {
  110. /*xhr.overrideMimeType(
  111. 'text/plain; charset=x-user-defined'
  112. );*/
  113. var text = xhr.responseText || xhr.response;
  114. if (!text) {
  115. return text;
  116. }
  117. try {
  118. return JSON.parse(text);
  119. } catch (e) {
  120. return text;
  121. }
  122. }
  123. }
  124. }
  125. </script>
  126. <style scoped>
  127. </style>