12f0521e5a4a5529675792541f246594ae5110531d41faea1b1525340bcd9d96bf00f5d3eaef1d284a89164cfbec532423bde577dbff9c4139344efaf2d8bc 2.5 KB

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