修复文件上传

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