第一次上传
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
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";
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* File 文件上传类型定义
|
||||
*/
|
||||
|
||||
/** 文件信息 */
|
||||
export interface FileInfo {
|
||||
/** 文件名称 */
|
||||
name: string;
|
||||
/** 文件URL */
|
||||
url: string;
|
||||
}
|
||||
Reference in New Issue
Block a user