uploader.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = this.options.previewData;
  68. if (!this.check(file)) return;
  69. const reader = new FileReader();
  70. reader.onload = (e) => {
  71. this.uploader();
  72. this.triggerFunc.call(this.options, this.options.onPreview)(e.target.result);
  73. }
  74. reader.readAsDataURL(file);
  75. }
  76. uploader () {
  77. const xhr = new XMLHttpRequest();
  78. let options = this.options;
  79. let formData = options.formData;
  80. if (xhr.upload) {
  81. xhr.upload.addEventListener('progress', (e) => {
  82. this.triggerFunc.call(options, options.onProgress)(formData, e.loaded, e.total);
  83. }, false);
  84. xhr.onreadystatechange = (e) => {
  85. if (xhr.readyState === 4) {
  86. if (xhr.status === options.xhrState) {
  87. this.triggerFunc.call(options, options.onSuccess)(formData, xhr.responseText);
  88. } else {
  89. this.triggerFunc.call(options, options.onFailure)(formData, xhr.responseText);
  90. }
  91. }
  92. };
  93. xhr.withCredentials = options.withCredentials;
  94. xhr.open('POST', options.url, true);
  95. // headers
  96. for (let key in options.headers) {
  97. xhr.setRequestHeader(key, options.headers[key])
  98. }
  99. this.triggerFunc.call(options, options.onStart)();
  100. xhr.send(formData);
  101. if(options.clearInput){
  102. options.$el.value = '' ;
  103. }
  104. } else {
  105. this.showMsg(this.xmlError)
  106. }
  107. }
  108. }
  109. export default IdaUploader;