鍒濆鎻愪氦锛氱墰鍙暟鎹鐞嗙郴缁?- 鍖呭惈鍚庣Spring Boot鍜屽墠绔疺ue3椤圭洰
This commit is contained in:
87
admin-system/node_modules/element-plus/es/components/upload/src/ajax.mjs
generated
vendored
Normal file
87
admin-system/node_modules/element-plus/es/components/upload/src/ajax.mjs
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
import { isNil } from 'lodash-unified';
|
||||
import { throwError } from '../../../utils/error.mjs';
|
||||
import { isArray } from '@vue/shared';
|
||||
|
||||
const SCOPE = "ElUpload";
|
||||
class UploadAjaxError extends Error {
|
||||
constructor(message, status, method, url) {
|
||||
super(message);
|
||||
this.name = "UploadAjaxError";
|
||||
this.status = status;
|
||||
this.method = method;
|
||||
this.url = url;
|
||||
}
|
||||
}
|
||||
function getError(action, option, xhr) {
|
||||
let msg;
|
||||
if (xhr.response) {
|
||||
msg = `${xhr.response.error || xhr.response}`;
|
||||
} else if (xhr.responseText) {
|
||||
msg = `${xhr.responseText}`;
|
||||
} else {
|
||||
msg = `fail to ${option.method} ${action} ${xhr.status}`;
|
||||
}
|
||||
return new UploadAjaxError(msg, xhr.status, option.method, action);
|
||||
}
|
||||
function getBody(xhr) {
|
||||
const text = xhr.responseText || xhr.response;
|
||||
if (!text) {
|
||||
return text;
|
||||
}
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch (e) {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
const ajaxUpload = (option) => {
|
||||
if (typeof XMLHttpRequest === "undefined")
|
||||
throwError(SCOPE, "XMLHttpRequest is undefined");
|
||||
const xhr = new XMLHttpRequest();
|
||||
const action = option.action;
|
||||
if (xhr.upload) {
|
||||
xhr.upload.addEventListener("progress", (evt) => {
|
||||
const progressEvt = evt;
|
||||
progressEvt.percent = evt.total > 0 ? evt.loaded / evt.total * 100 : 0;
|
||||
option.onProgress(progressEvt);
|
||||
});
|
||||
}
|
||||
const formData = new FormData();
|
||||
if (option.data) {
|
||||
for (const [key, value] of Object.entries(option.data)) {
|
||||
if (isArray(value) && value.length)
|
||||
formData.append(key, ...value);
|
||||
else
|
||||
formData.append(key, value);
|
||||
}
|
||||
}
|
||||
formData.append(option.filename, option.file, option.file.name);
|
||||
xhr.addEventListener("error", () => {
|
||||
option.onError(getError(action, option, xhr));
|
||||
});
|
||||
xhr.addEventListener("load", () => {
|
||||
if (xhr.status < 200 || xhr.status >= 300) {
|
||||
return option.onError(getError(action, option, xhr));
|
||||
}
|
||||
option.onSuccess(getBody(xhr));
|
||||
});
|
||||
xhr.open(option.method, action, true);
|
||||
if (option.withCredentials && "withCredentials" in xhr) {
|
||||
xhr.withCredentials = true;
|
||||
}
|
||||
const headers = option.headers || {};
|
||||
if (headers instanceof Headers) {
|
||||
headers.forEach((value, key) => xhr.setRequestHeader(key, value));
|
||||
} else {
|
||||
for (const [key, value] of Object.entries(headers)) {
|
||||
if (isNil(value))
|
||||
continue;
|
||||
xhr.setRequestHeader(key, String(value));
|
||||
}
|
||||
}
|
||||
xhr.send(formData);
|
||||
return xhr;
|
||||
};
|
||||
|
||||
export { UploadAjaxError, ajaxUpload };
|
||||
//# sourceMappingURL=ajax.mjs.map
|
||||
Reference in New Issue
Block a user