uploader.js 3.6 KB

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