feat: 新增文件上传组件

新增文件上传组件
This commit is contained in:
胡少翔
2024-07-24 14:08:34 +08:00
parent 4a986b35f2
commit 95ac5e4558
4 changed files with 479 additions and 10 deletions

View File

@@ -1,6 +1,11 @@
import request from "@/utils/request";
class FileAPI {
/**
* 文件上传地址
*/
static uploadUrl = import.meta.env.VITE_APP_BASE_API + "/api/v1/files";
/**
* 上传文件
*
@@ -31,6 +36,27 @@ class FileAPI {
params: { filePath: filePath },
});
}
/**
* 下载文件
* @param url
* @param fileName
*/
static downloadFile(url: string, fileName?: string) {
return request({
url: url,
method: "get",
responseType: "blob",
}).then((res) => {
const blob = new Blob([res.data]);
const a = document.createElement("a");
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName || "下载文件";
a.click();
window.URL.revokeObjectURL(url);
});
}
}
export default FileAPI;