ajax.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var lodashUnified = require('lodash-unified');
  4. var error = require('../../../utils/error.js');
  5. var shared = require('@vue/shared');
  6. const SCOPE = "ElUpload";
  7. class UploadAjaxError extends Error {
  8. constructor(message, status, method, url) {
  9. super(message);
  10. this.name = "UploadAjaxError";
  11. this.status = status;
  12. this.method = method;
  13. this.url = url;
  14. }
  15. }
  16. function getError(action, option, xhr) {
  17. let msg;
  18. if (xhr.response) {
  19. msg = `${xhr.response.error || xhr.response}`;
  20. } else if (xhr.responseText) {
  21. msg = `${xhr.responseText}`;
  22. } else {
  23. msg = `fail to ${option.method} ${action} ${xhr.status}`;
  24. }
  25. return new UploadAjaxError(msg, xhr.status, option.method, action);
  26. }
  27. function getBody(xhr) {
  28. const text = xhr.responseText || xhr.response;
  29. if (!text) {
  30. return text;
  31. }
  32. try {
  33. return JSON.parse(text);
  34. } catch (e) {
  35. return text;
  36. }
  37. }
  38. const ajaxUpload = (option) => {
  39. if (typeof XMLHttpRequest === "undefined")
  40. error.throwError(SCOPE, "XMLHttpRequest is undefined");
  41. const xhr = new XMLHttpRequest();
  42. const action = option.action;
  43. if (xhr.upload) {
  44. xhr.upload.addEventListener("progress", (evt) => {
  45. const progressEvt = evt;
  46. progressEvt.percent = evt.total > 0 ? evt.loaded / evt.total * 100 : 0;
  47. option.onProgress(progressEvt);
  48. });
  49. }
  50. const formData = new FormData();
  51. if (option.data) {
  52. for (const [key, value] of Object.entries(option.data)) {
  53. if (shared.isArray(value) && value.length)
  54. formData.append(key, ...value);
  55. else
  56. formData.append(key, value);
  57. }
  58. }
  59. formData.append(option.filename, option.file, option.file.name);
  60. xhr.addEventListener("error", () => {
  61. option.onError(getError(action, option, xhr));
  62. });
  63. xhr.addEventListener("load", () => {
  64. if (xhr.status < 200 || xhr.status >= 300) {
  65. return option.onError(getError(action, option, xhr));
  66. }
  67. option.onSuccess(getBody(xhr));
  68. });
  69. xhr.open(option.method, action, true);
  70. if (option.withCredentials && "withCredentials" in xhr) {
  71. xhr.withCredentials = true;
  72. }
  73. const headers = option.headers || {};
  74. if (headers instanceof Headers) {
  75. headers.forEach((value, key) => xhr.setRequestHeader(key, value));
  76. } else {
  77. for (const [key, value] of Object.entries(headers)) {
  78. if (lodashUnified.isNil(value))
  79. continue;
  80. xhr.setRequestHeader(key, String(value));
  81. }
  82. }
  83. xhr.send(formData);
  84. return xhr;
  85. };
  86. exports.UploadAjaxError = UploadAjaxError;
  87. exports.ajaxUpload = ajaxUpload;
  88. //# sourceMappingURL=ajax.js.map