feat: 新增参数method,若使用PUT上传则不适用formData
This commit is contained in:
parent
43b1f61f4a
commit
4f698ce772
|
|
@ -1,29 +1,6 @@
|
|||
|
||||
|
||||
export class Upload {
|
||||
// async uploadFile(
|
||||
// file: File | string,
|
||||
// name: string,
|
||||
// uploadUrl: string,
|
||||
// formData: Record<string, any>,
|
||||
// autoInform?: boolean
|
||||
// ): Promise<any> {
|
||||
// const formData2 = new FormData();
|
||||
// for (const key of Object.keys(formData)) {
|
||||
// formData2.append(key, formData[key]);
|
||||
// }
|
||||
// formData2.append(name || 'file', file as File);
|
||||
|
||||
// const options = {
|
||||
// body: formData2,
|
||||
// method: 'POST',
|
||||
// };
|
||||
|
||||
// const result = await fetch(uploadUrl, options);
|
||||
// return result;
|
||||
// }
|
||||
|
||||
|
||||
async uploadFile(
|
||||
file: File | string,
|
||||
name: string,
|
||||
|
|
@ -31,20 +8,17 @@ export class Upload {
|
|||
formData: Record<string, any>,
|
||||
autoInform?: boolean,
|
||||
getPercent?: Function,
|
||||
method: "POST" | "PUT" | "PATCH" = "POST"
|
||||
): Promise<any> {
|
||||
const isPut = method === "PUT";
|
||||
|
||||
// 进度监听模式
|
||||
if (getPercent) {
|
||||
|
||||
const formData2 = new FormData();
|
||||
Object.entries(formData).forEach(([key, value]) => {
|
||||
formData2.append(key, value);
|
||||
});
|
||||
formData2.append(name || 'file', file);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
let percent = 0;
|
||||
|
||||
xhr.upload.addEventListener('progress', (event) => {
|
||||
xhr.upload.addEventListener("progress", (event) => {
|
||||
if (event.lengthComputable) {
|
||||
percent = Math.round((event.loaded / event.total) * 100);
|
||||
getPercent(percent);
|
||||
|
|
@ -54,12 +28,12 @@ export class Upload {
|
|||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
if (xhr.status === 204) {
|
||||
resolve({ status: 204 }); // 正确返回状态码
|
||||
resolve({ status: 204 });
|
||||
} else {
|
||||
try {
|
||||
const data = JSON.parse(xhr.responseText);
|
||||
resolve(data);
|
||||
} catch (e) {
|
||||
} catch {
|
||||
resolve({ status: xhr.status, raw: xhr.responseText });
|
||||
}
|
||||
}
|
||||
|
|
@ -68,25 +42,53 @@ export class Upload {
|
|||
}
|
||||
};
|
||||
|
||||
xhr.onerror = () => reject(new Error('Network Error'));
|
||||
xhr.open('POST', uploadUrl);
|
||||
xhr.send(formData2);
|
||||
xhr.onerror = () => reject(new Error("Network Error"));
|
||||
xhr.open(method, uploadUrl);
|
||||
|
||||
if (isPut) {
|
||||
// PUT 模式:直接上传文件
|
||||
if (file instanceof File) {
|
||||
xhr.setRequestHeader("Content-Type", file.type || "application/octet-stream");
|
||||
}
|
||||
xhr.send(file as any);
|
||||
} else {
|
||||
// POST / PATCH 模式:构建表单
|
||||
const formData2 = new FormData();
|
||||
Object.entries(formData).forEach(([key, value]) => {
|
||||
formData2.append(key, value);
|
||||
});
|
||||
formData2.append(name || "file", file as File);
|
||||
xhr.send(formData2);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 无进度监听模式(直接 fetch)
|
||||
if (isPut) {
|
||||
// S3 预签名上传
|
||||
const headers: Record<string, string> = {};
|
||||
if (file instanceof File) {
|
||||
headers["Content-Type"] = file.type || "application/octet-stream";
|
||||
}
|
||||
|
||||
const result = await fetch(uploadUrl, {
|
||||
method: "PUT",
|
||||
headers,
|
||||
body: file as any,
|
||||
});
|
||||
return result;
|
||||
} else {
|
||||
|
||||
|
||||
// 表单上传
|
||||
const formData2 = new FormData();
|
||||
for (const key of Object.keys(formData)) {
|
||||
formData2.append(key, formData[key]);
|
||||
}
|
||||
formData2.append(name || 'file', file as File);
|
||||
formData2.append(name || "file", file as File);
|
||||
|
||||
const options = {
|
||||
const result = await fetch(uploadUrl, {
|
||||
method,
|
||||
body: formData2,
|
||||
method: 'POST',
|
||||
};
|
||||
|
||||
const result = await fetch(uploadUrl, options);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue