修复文件上传

This commit is contained in:
zhonghua 2026-03-19 10:53:13 +08:00
parent deb8431910
commit 2eb4a28b89

View File

@ -1,4 +1,5 @@
import { http } from "./index"; import { http } from "./index";
import axios from "axios";
import { buildOssDirPath } from "@/utils/env"; import { buildOssDirPath } from "@/utils/env";
/** 上传请求 AbortController 映射,用于取消上传 */ /** 上传请求 AbortController 映射,用于取消上传 */
@ -80,13 +81,11 @@ export const fileApi = {
// 自动添加环境前缀 // 自动添加环境前缀
const fullDir = buildOssDirPath(dir); const fullDir = buildOssDirPath(dir);
const response = await http.get<{ data: OssToken }>( const response = await http.get<OssToken>(`/v1/files/oss/token`, {
`/v1/files/oss/token`, params: { fileName, dir: fullDir },
{ });
params: { fileName, dir: fullDir }, console.log("getOssToken", response);
}, return response;
);
return response.data;
}, },
/** /**
@ -109,9 +108,7 @@ export const fileApi = {
}, },
): Promise<{ url: string }> => { ): Promise<{ url: string }> => {
const opts = const opts =
typeof options === "function" typeof options === "function" ? { onProgress: options } : (options ?? {});
? { onProgress: options }
: options ?? {};
const formData = new FormData(); const formData = new FormData();
// 按照阿里云 OSS PostObject 要求构造表单 // 按照阿里云 OSS PostObject 要求构造表单
@ -125,7 +122,7 @@ export const fileApi = {
const controller = getUploadController(file); const controller = getUploadController(file);
const signal = opts.signal ?? controller.signal; const signal = opts.signal ?? controller.signal;
console.log(token.host, formData, signal);
try { try {
await axios.post(token.host, formData, { await axios.post(token.host, formData, {
headers: { headers: {
@ -145,10 +142,12 @@ export const fileApi = {
} }
}, },
}); });
return { return {
url: `${token.host}/${token.key}`, url: `${token.host}/${token.key}`,
}; };
} catch (error) {
console.error(error);
throw error;
} finally { } finally {
abortUpload(file); abortUpload(file);
} }
@ -204,12 +203,9 @@ export const fileApi = {
* *
*/ */
deleteFile: async (filePath: string): Promise<DeleteResult> => { deleteFile: async (filePath: string): Promise<DeleteResult> => {
const response = await http.post<DeleteResult>( const response = await http.post<DeleteResult>(`/v1/files/delete`, {
`/v1/files/delete`, filePath,
{ });
filePath,
},
);
return response; return response;
}, },