Files
xxk-proxy/ui/src/api/file/index.ts
T
2026-06-11 10:31:24 +08:00

64 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import request from "@/utils/request";
import type { FileInfo } from "./types";
const FileAPI = {
/** 上传文件 (传入 FormData,上传进度回调) */
upload(formData: FormData, onProgress?: (percent: number) => void) {
return request<any, FileInfo>({
url: "/api/v1/files",
method: "post",
data: formData,
headers: { "Content-Type": "multipart/form-data" },
onUploadProgress: (progressEvent) => {
if (progressEvent.total) {
const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total);
onProgress?.(percent);
}
},
});
},
/** 上传文件(传入 File */
uploadFile(file: File) {
const formData = new FormData();
formData.append("file", file);
return request<any, FileInfo>({
url: "/api/v1/files",
method: "post",
data: formData,
headers: { "Content-Type": "multipart/form-data" },
});
},
/** 删除文件 */
delete(filePath?: string) {
return request({
url: "/api/v1/files",
method: "delete",
params: { filePath },
});
},
/** 下载文件 */
download(url: string, fileName?: string) {
return request({
url,
method: "get",
responseType: "blob",
}).then((res) => {
const blob = new Blob([res.data]);
const a = document.createElement("a");
const urlObject = window.URL.createObjectURL(blob);
a.href = urlObject;
a.download = fileName || "下载文件";
a.click();
window.URL.revokeObjectURL(urlObject);
});
},
};
export default FileAPI;
// 重导出类型
export * from "./types";