新增useImportFile

master
LiuJiaNan 2025-10-30 17:38:25 +08:00
parent d68bd52ba6
commit c54bd2eb68
4 changed files with 75 additions and 5 deletions

View File

@ -10,11 +10,13 @@ export default function useDownloadBlob() {
const [loading, setLoading] = useState(false);
// 下载Blob流文件
const downloadBlob = (url, options = { name: "", type: "", params: {} }) => {
const downloadBlob = (url, options) => {
setLoading(true);
return new Promise((resolve, reject) => {
const { name= "", type= "", params= {} } = options
const finalUrl = (process.env.app.API_HOST || window.__JJB_ENVIRONMENT__.API_HOST) + url;
Object.entries(options.params).forEach(([key, value]) => {
Object.entries(params).forEach(([key, value]) => {
finalUrl.searchParams.append(key, value);
});
fetch(finalUrl, {
@ -32,14 +34,14 @@ export default function useDownloadBlob() {
})
.then((blob) => {
const finalBlob = new Blob([blob], {
type: options.type || "application/vnd.ms-excel",
type: type || "application/vnd.ms-excel",
});
const downloadElement = document.createElement("a");
const href = window.URL.createObjectURL(finalBlob);
downloadElement.style.display = "none";
downloadElement.href = href;
downloadElement.download
= options.name || dayjs().format("YYYY-MM-DD HH:mm:ss");
= name || dayjs().format("YYYY-MM-DD HH:mm:ss");
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);

24
hooks/useImportFile/index.d.ts vendored Normal file
View File

@ -0,0 +1,24 @@
export interface UploadFile {
/** 原始文件对象 */
originFileObj?: File;
[key: string]: any;
}
interface UseImportFileOptions {
/** 要上传的文件数组 */
files: UploadFile[];
/** 额外携带的参数对象 */
params?: Record<string, any>;
}
export interface ImportFileOptions {
url: string;
options: UseImportFileOptions;
}
export type ImportFileFunction = (url: string, options: UseImportFileOptions) => Promise<any>;
/**
*
*/
export default function useImportFile(): [boolean, ImportFileFunction];

View File

@ -0,0 +1,44 @@
import {request} from "@cqsjjb/jjb-common-lib/http";
import { useState } from "react";
/**
* 导入文件
*/
export default function useImportFile() {
// loading状态
const [loading, setLoading] = useState(false);
// 导入文件
const importFile = (url, options) => {
setLoading(true);
return new Promise((resolve, reject) => {
const { files = [], params = {} } = options
const formData = new FormData();
// 将文件添加到formData中
files.forEach((f) => {
f.originFileObj && formData.append("file", f.originFileObj);
});
// 将额外携带的参数添加到formData中
Object.keys(params).forEach((key) => {
formData.append(key, params[key]);
});
// 发送请求
request(url, "post", formData, { "Content-Type": "multipart/form-data" })
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
})
.finally(() => {
setLoading(false);
});
});
};
return [loading, importFile];
}

View File

@ -6,7 +6,7 @@ export interface UploadFile {
export interface SingleUploadFileOptions {
/** 要上传的文件 */
files: UploadFile;
files: UploadFile[];
/** 是否单文件上传 */
single?: true;
/** 上传的参数 */