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.
108 lines
2.7 KiB
108 lines
2.7 KiB
3 years ago
|
import type { UploadRequestOption, UploadRequestError, UploadProgressEvent } from './interface';
|
||
|
|
||
|
function getError(option: UploadRequestOption, xhr: XMLHttpRequest) {
|
||
5 years ago
|
const msg = `cannot ${option.method} ${option.action} ${xhr.status}'`;
|
||
3 years ago
|
const err = new Error(msg) as UploadRequestError;
|
||
6 years ago
|
err.status = xhr.status;
|
||
5 years ago
|
err.method = option.method;
|
||
6 years ago
|
err.url = option.action;
|
||
|
return err;
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
function getBody(xhr: XMLHttpRequest) {
|
||
6 years ago
|
const text = xhr.responseText || xhr.response;
|
||
7 years ago
|
if (!text) {
|
||
6 years ago
|
return text;
|
||
7 years ago
|
}
|
||
|
|
||
|
try {
|
||
6 years ago
|
return JSON.parse(text);
|
||
7 years ago
|
} catch (e) {
|
||
6 years ago
|
return text;
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
export default function upload(option: UploadRequestOption) {
|
||
|
// eslint-disable-next-line no-undef
|
||
|
const xhr = new XMLHttpRequest();
|
||
7 years ago
|
|
||
|
if (option.onProgress && xhr.upload) {
|
||
3 years ago
|
xhr.upload.onprogress = function progress(e: UploadProgressEvent) {
|
||
7 years ago
|
if (e.total > 0) {
|
||
6 years ago
|
e.percent = (e.loaded / e.total) * 100;
|
||
7 years ago
|
}
|
||
6 years ago
|
option.onProgress(e);
|
||
|
};
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
// eslint-disable-next-line no-undef
|
||
|
const formData = new FormData();
|
||
7 years ago
|
|
||
|
if (option.data) {
|
||
5 years ago
|
Object.keys(option.data).forEach(key => {
|
||
|
const value = option.data[key];
|
||
|
// support key-value array data
|
||
|
if (Array.isArray(value)) {
|
||
|
value.forEach(item => {
|
||
|
// { list: [ 11, 22 ] }
|
||
|
// formData.append('list[]', 11);
|
||
|
formData.append(`${key}[]`, item);
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
3 years ago
|
formData.append(key, value as string | Blob);
|
||
6 years ago
|
});
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
// eslint-disable-next-line no-undef
|
||
|
if (option.file instanceof Blob) {
|
||
|
formData.append(option.filename, option.file, (option.file as any).name);
|
||
|
} else {
|
||
|
formData.append(option.filename, option.file);
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
xhr.onerror = function error(e) {
|
||
|
option.onError(e);
|
||
|
};
|
||
7 years ago
|
|
||
6 years ago
|
xhr.onload = function onload() {
|
||
7 years ago
|
// allow success when 2xx status
|
||
|
// see https://github.com/react-component/upload/issues/34
|
||
|
if (xhr.status < 200 || xhr.status >= 300) {
|
||
6 years ago
|
return option.onError(getError(option, xhr), getBody(xhr));
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
return option.onSuccess(getBody(xhr), xhr);
|
||
6 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
xhr.open(option.method, option.action, true);
|
||
7 years ago
|
|
||
|
// Has to be after `.open()`. See https://github.com/enyo/dropzone/issues/179
|
||
|
if (option.withCredentials && 'withCredentials' in xhr) {
|
||
6 years ago
|
xhr.withCredentials = true;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
const headers = option.headers || {};
|
||
7 years ago
|
|
||
|
// when set headers['X-Requested-With'] = null , can close default XHR header
|
||
|
// see https://github.com/react-component/upload/issues/33
|
||
|
if (headers['X-Requested-With'] !== null) {
|
||
6 years ago
|
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
Object.keys(headers).forEach(h => {
|
||
|
if (headers[h] !== null) {
|
||
6 years ago
|
xhr.setRequestHeader(h, headers[h]);
|
||
7 years ago
|
}
|
||
3 years ago
|
});
|
||
|
|
||
6 years ago
|
xhr.send(formData);
|
||
7 years ago
|
|
||
|
return {
|
||
6 years ago
|
abort() {
|
||
|
xhr.abort();
|
||
7 years ago
|
},
|
||
6 years ago
|
};
|
||
7 years ago
|
}
|