uploader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. class IdaUploader {
  2. constructor (settings) {
  3. this.options = {
  4. url: '',
  5. formData: null,
  6. headers: {}, //自定义headers
  7. withCredentials:false,//支持发送 cookie 凭证信息
  8. isPreview: true, //是否开启本地预览
  9. previewData: null,
  10. maxSize: 0, //允许上传的文件最大字节,0为不限制
  11. acceptType: [], //允许上传的文件类型,如'image/jpeg'
  12. showMsgFn: null,
  13. onStart: null,
  14. onProgress: null,
  15. onPreview: null,
  16. onSuccess: null,
  17. onFailure: null,
  18. xhrStatus:200, //默认上传成功是200
  19. readyState:4,
  20. xmlError:null,
  21. typeError:null,
  22. limitError:null
  23. };
  24. Object.assign(this.options, settings);
  25. this[this.options.isPreview ? 'preview' : 'uploader']()
  26. }
  27. triggerFunc(func) {
  28. if (typeof(func)==='function') {
  29. return func.bind(this);
  30. } else {
  31. console.warn(func + 'is not a function!');
  32. return function() {};
  33. }
  34. }
  35. showMsg (msg) {
  36. if (typeof(this.options.showMsgFn)=='function') {
  37. this.options.showMsgFn(msg);
  38. } else {
  39. console.log(msg);
  40. }
  41. }
  42. check (file) {
  43. if(Array.isArray(file)){
  44. for(let key in file){
  45. if (this.options.maxSize && (file[key].size > this.options.maxSize)) {
  46. this.showMsg(this.options.limitError);
  47. return false;
  48. }
  49. if (this.options.acceptType.length && this.options.acceptType.indexOf(file[key].type) === -1) {
  50. this.showMsg(this.options.typeError);
  51. return false;
  52. }
  53. }
  54. }else{
  55. if (this.options.maxSize && (file.size > this.options.maxSize)) {
  56. this.showMsg(this.options.limitError);
  57. return false;
  58. }
  59. if (this.options.acceptType.length && this.options.acceptType.indexOf(file.type) === -1) {
  60. this.showMsg(this.options.typeError);
  61. return false;
  62. }
  63. }
  64. return true;
  65. }
  66. preview () {
  67. const file = Array.from(this.options.previewData);
  68. if (!this.check(file)) return;
  69. let promArray = []
  70. file.map(item=>{
  71. let temp = new Promise((resolve,reject)=>{
  72. const reader = new FileReader();
  73. reader.readAsDataURL(item);
  74. reader.onload = (e) => {
  75. this.uploader();
  76. resolve(e)
  77. }
  78. })
  79. promArray.push(temp)
  80. });
  81. Promise.all(promArray).then(res=>{
  82. console.log(res)
  83. let out = [];
  84. if(res){
  85. res.map(item=>{
  86. out.push(item.target.result)
  87. })
  88. }
  89. this.triggerFunc.call(this.options, this.options.onPreview)(out);
  90. })
  91. }
  92. uploader () {
  93. const xhr = new XMLHttpRequest();
  94. let options = this.options;
  95. let formData = options.formData;
  96. if (xhr.upload) {
  97. xhr.upload.addEventListener('progress', (e) => {
  98. this.triggerFunc.call(options, options.onProgress)(formData, e.loaded, e.total);
  99. }, false);
  100. xhr.onreadystatechange = (e) => {
  101. if (xhr.readyState === 4) {
  102. if (xhr.status === options.xhrState) {
  103. this.triggerFunc.call(options, options.onSuccess)(formData, xhr.responseText);
  104. } else {
  105. this.triggerFunc.call(options, options.onFailure)(formData, xhr.responseText);
  106. }
  107. }
  108. };
  109. xhr.withCredentials = options.withCredentials;
  110. xhr.open('POST', options.url, true);
  111. // headers
  112. for (let key in options.headers) {
  113. xhr.setRequestHeader(key, options.headers[key])
  114. }
  115. this.triggerFunc.call(options, options.onStart)();
  116. xhr.send(formData);
  117. if(options.clearInput){
  118. options.$el.value = '' ;
  119. }
  120. } else {
  121. this.showMsg(this.xmlError)
  122. }
  123. }
  124. }
  125. export default IdaUploader;