From 4f698ce77238c9363f4a82b0468c7f32f3e9cee6 Mon Sep 17 00:00:00 2001 From: QCQCQC <1220204124@zust.edu.cn> Date: Wed, 15 Oct 2025 16:23:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=8F=82=E6=95=B0met?= =?UTF-8?q?hod=EF=BC=8C=E8=8B=A5=E4=BD=BF=E7=94=A8PUT=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=88=99=E4=B8=8D=E9=80=82=E7=94=A8formData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/upload.web.ts | 90 +++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/src/utils/upload.web.ts b/src/utils/upload.web.ts index 68b7e731..cf907d0e 100644 --- a/src/utils/upload.web.ts +++ b/src/utils/upload.web.ts @@ -1,29 +1,6 @@ export class Upload { - // async uploadFile( - // file: File | string, - // name: string, - // uploadUrl: string, - // formData: Record, - // autoInform?: boolean - // ): Promise { - // 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, autoInform?: boolean, getPercent?: Function, + method: "POST" | "PUT" | "PATCH" = "POST" ): Promise { + 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 = {}; + 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; } }