You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
6.2 KiB

3 months ago
  1. {"version":3,"file":"ajax.mjs","sources":["../../../../../../packages/components/upload/src/ajax.ts"],"sourcesContent":["import { isNil } from 'lodash-unified'\nimport { isArray, throwError } from '@element-plus/utils'\nimport type {\n UploadProgressEvent,\n UploadRequestHandler,\n UploadRequestOptions,\n} from './upload'\n\nconst SCOPE = 'ElUpload'\n\nexport class UploadAjaxError extends Error {\n name = 'UploadAjaxError'\n status: number\n method: string\n url: string\n\n constructor(message: string, status: number, method: string, url: string) {\n super(message)\n this.status = status\n this.method = method\n this.url = url\n }\n}\n\nfunction getError(\n action: string,\n option: UploadRequestOptions,\n xhr: XMLHttpRequest\n) {\n let msg: string\n if (xhr.response) {\n msg = `${xhr.response.error || xhr.response}`\n } else if (xhr.responseText) {\n msg = `${xhr.responseText}`\n } else {\n msg = `fail to ${option.method} ${action} ${xhr.status}`\n }\n\n return new UploadAjaxError(msg, xhr.status, option.method, action)\n}\n\nfunction getBody(xhr: XMLHttpRequest): XMLHttpRequestResponseType {\n const text = xhr.responseText || xhr.response\n if (!text) {\n return text\n }\n\n try {\n return JSON.parse(text)\n } catch {\n return text\n }\n}\n\nexport const ajaxUpload: UploadRequestHandler = (option) => {\n if (typeof XMLHttpRequest === 'undefined')\n throwError(SCOPE, 'XMLHttpRequest is undefined')\n\n const xhr = new XMLHttpRequest()\n const action = option.action\n\n if (xhr.upload) {\n xhr.upload.addEventListener('progress', (evt) => {\n const progressEvt = evt as UploadProgressEvent\n progressEvt.percent = evt.total > 0 ? (evt.loaded / evt.total) * 100 : 0\n option.onProgress(progressEvt)\n })\n }\n\n const formData = new FormData()\n if (option.data) {\n for (const [key, value] of Object.entries(option.data)) {\n if (isArray(value) && value.length) formData.append(key, ...value)\n else formData.append(key, value)\n }\n }\n formData.append(option.filename, option.file, option.file.name)\n\n xhr.addEventListener('error', () => {\n option.onError(getError(action, option, xhr))\n })\n\n xhr.addEventListener('load', () => {\n if (xhr.status < 200 || xhr.status >= 300) {\n return option.onError(getError(action, option, xhr))\n }\n option.onSuccess(getBody(xhr))\n })\n\n xhr.open(option.method, action, true)\n\n if (option.withCredentials && 'withCredentials' in xhr) {\n xhr.withCredentials = true\n }\n\n const headers = option.headers || {}\n if (headers instanceof Headers) {\n headers.forEach((value, key) => xhr.setRequestHeader(key, value))\n } else {\n for (const [key, value] of Object.entries(headers)) {\n if (isNil(value)) continue\n xhr.setRequestHeader(key, String(value))\n }\n }\n\n xhr.send(formData)\n return xhr\n}\n"],"names":[],"mappings":";;;;AAEA,MAAM,KAAK,GAAG,UAAU,CAAC;AAClB,MAAM,eAAe,SAAS,KAAK,CAAC;AAC3C,EAAE,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE;AAC5C,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AACnB,IAAI,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;AAClC,IAAI,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;AACzB,IAAI,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;AACzB,IAAI,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;AACnB,GAAG;AACH,CAAC;AACD,SAAS,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE;AACvC,EAAE,IAAI,GAAG,CAAC;AACV,EAAE,IAAI,GAAG,CAAC,QAAQ,EAAE;AACpB,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAC,KAAK,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;AAClD,GAAG,MAAM,IAAI,GAAG,CAAC,YAAY,EAAE;AAC/B,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC;AAChC,GAAG,MAAM;AACT,IAAI,GAAG,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,E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