优化useDownloadFile

master
LiuJiaNan 2025-10-31 14:26:46 +08:00
parent 4c650b93e8
commit 5d4e086c52
5 changed files with 75 additions and 30 deletions

View File

@ -14,6 +14,9 @@ export default function useDownloadBlob(returnType = "object") {
setLoading(true);
return new Promise((resolve, reject) => {
if (!url)
return reject(new Error("请传入 url"));
const { name = "", type = "", params = {} } = options;
const finalUrl = (process.env.app.API_HOST || window.__JJB_ENVIRONMENT__.API_HOST) + url;
Object.entries(params).forEach(([key, value]) => {

View File

@ -1,4 +1,14 @@
export interface DownloadFileOptions {
/** 下载文件的URL */
url: string;
/** 下载文件的自定义文件名 */
name?: string;
}
export type DownloadFileFunction = (options: DownloadFileOptions) => void;
/**
*
*/
export default function useDownloadFile(url: string, name?: string): void;
export default function useDownloadFile(returnType: "array"): [boolean, DownloadFileFunction];
export default function useDownloadFile(returnType?: "object"): { loading: boolean; downloadFile: DownloadFileFunction };

View File

@ -1,36 +1,62 @@
import { message, Modal } from "antd";
import { getFileName, getFileSuffix, getFileUrl } from "../../utils/index.js";
import { useState } from "react";
/**
* 下载文件
*/
export default function useDownloadFile(url, name) {
if (!url)
throw new Error("没有下载地址");
Modal.confirm({ title: "提示", content: "确定要下载此文件吗?", onOk: () => {
const fileUrl = getFileUrl();
if (name) {
if (!getFileSuffix(url))
name = name + getFileSuffix(url);
}
else {
name = getFileName(url);
}
fetch(!url.includes(fileUrl) ? fileUrl + url : url)
.then(res => res.blob())
.then((blob) => {
const a = document.createElement("a");
document.body.appendChild(a);
a.style.display = "none";
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = `${name}`;
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
})
.catch(() => {
message.error("下载失败");
});
} });
export default function useDownloadFile(returnType = "object") {
// loading状态
const [loading, setLoading] = useState(false);
// 下载文件
const downloadFile = (options) => {
setLoading(true);
const { url, name: fileName } = options;
if (!url)
throw new Error("请传入 url");
Modal.confirm({
title: "提示",
content: "确定要下载此文件吗?",
onOk: () => {
const fileUrl = getFileUrl();
let name = fileName;
if (name) {
if (!getFileSuffix(name) && getFileSuffix(url)) {
name = `${name}.${getFileSuffix(url)}`;
}
}
else {
name = getFileName(url);
}
fetch(!url.includes(fileUrl) ? fileUrl + url : url)
.then(res => res.blob())
.then((blob) => {
const a = document.createElement("a");
document.body.appendChild(a);
a.style.display = "none";
const urlObject = window.URL.createObjectURL(blob);
a.href = urlObject;
a.download = `${name}`;
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(urlObject);
})
.catch(() => {
message.error("下载失败");
})
.finally(() => {
setLoading(false);
});
},
});
};
if (returnType === "array")
return [loading, downloadFile];
return { loading, downloadFile };
}

View File

@ -13,6 +13,9 @@ export default function useImportFile(returnType = "object") {
setLoading(true);
return new Promise((resolve, reject) => {
if (!url)
return reject(new Error("请传入 url"));
const { files = [], params = {} } = options;
const formData = new FormData();

View File

@ -101,6 +101,9 @@ function getQuery(keysStr, valuesStr) {
* 自定义 useTable继承 ahooks useAntdTable根据需求进行扩展
*/
function useTable(service, options) {
if (!service)
throw new Error("请传入 service");
// 获取额外参数和转换函数
const { params: extraParams, transform, ...restOptions } = options || {};