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.
1 line
5.4 KiB
1 line
5.4 KiB
11 months ago
|
{"version":3,"file":"ajax.mjs","sources":["../../../../../../packages/components/upload/src/ajax.ts"],"sourcesContent":["import { hasOwn } from '@vue/shared'\nimport type {\n ElUploadProgressEvent,\n ElUploadRequestOptions,\n ElUploadAjaxError,\n} from './upload.type'\n\nfunction getError(\n action: string,\n option: ElUploadRequestOptions,\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 const err = new Error(msg) as ElUploadAjaxError\n err.status = xhr.status\n err.method = option.method\n err.url = action\n return err\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 (e) {\n return text\n }\n}\n\nexport default function upload(option: ElUploadRequestOptions) {\n if (typeof XMLHttpRequest === 'undefined') {\n return\n }\n\n const xhr = new XMLHttpRequest()\n const action = option.action\n\n if (xhr.upload) {\n xhr.upload.onprogress = function progress(e) {\n if (e.total > 0) {\n ;(e as ElUploadProgressEvent).percent = (e.loaded / e.total) * 100\n }\n option.onProgress(e)\n }\n }\n\n const formData = new FormData()\n\n if (option.data) {\n Object.keys(option.data).forEach((key) => {\n formData.append(key, option.data[key])\n })\n }\n\n formData.append(option.filename, option.file, option.file.name)\n\n xhr.onerror = function error() {\n option.onError(getError(action, option, xhr))\n }\n\n xhr.onload = function onload() {\n if (xhr.status < 200 || xhr.status >= 300) {\n return option.onError(getError(action, option, xhr))\n }\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\n for (const item in headers) {\n if (hasOwn(headers, item) && headers[item] !== null) {\n xhr.setRequestHeader(item, headers[item])\n }\n }\n\n if (headers instanceof Headers) {\n headers.forEach((value, key) => {\n xhr.setRequestHeader(key, value)\n })\n }\n\n xhr.send(formData)\n return xhr\n}\n"],"names":[],"mappings":";;AACA,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,EAAE,MAAM,CAAC,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7D,GAAG;AACH,EAAE,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;AAC7B,EAAE,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;AAC1B,EAAE,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;AAC7B,EAAE,GAAG,CAAC,GAAG,GAAG,MAAM,CAAC;AACnB,EAAE,OAAO,GAAG,CAAC;AACb,CAAC;AACD,SAAS,OAAO,CAAC,GAAG,EAAE;AACtB,EAAE,MAAM,IAAI,GAAG,GAAG,CAAC,YAAY,IAAI,GAAG,CAAC,QAAQ,CAAC;AAChD,EAAE,IAAI,CAAC,IAAI,EAAE;AACb,IAAI,OAAO,IAAI,CAAC;AAChB,GAAG;AACH,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,KAAK,CA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