Skip to content

Commit bca7d67

Browse files
committed
feat: Complete the result export
1 parent a198866 commit bca7d67

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

chat2db-client/src/pages/main/workspace/components/WorkspaceRightItem/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { v4 as uuidV4 } from 'uuid';
1515
import sql from '@/service/sql';
1616
import { isNumber } from 'lodash';
1717
import { ExportSizeEnum, ExportTypeEnum } from '@/typings/resultTable';
18+
import { downloadFile } from '@/utils/common';
1819
interface IProps {
1920
className?: string;
2021
isActive: boolean;
@@ -146,8 +147,7 @@ const WorkspaceRightItem = memo<IProps>(function (props) {
146147
exportSize: ExportSizeEnum,
147148
) => {
148149
const params: IExportParams = { ...data, sql, originalSql, exportType, exportSize };
149-
150-
await sqlServer.exportResultTable(params);
150+
downloadFile(window._BaseURL + '/api/rdb/dml/export', params);
151151
};
152152

153153
const handleResultTabEdit = (type: 'add' | 'remove', uuid?: string | number) => {

chat2db-client/src/service/sql.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export interface IExportParams extends IExecuteSqlParams {
123123
/**
124124
* 导出-表格
125125
*/
126-
const exportResultTable = createRequest<IExportParams, any>('/api/rdb/dml/export', { method: 'post' });
126+
// const exportResultTable = createRequest<IExportParams, any>('/api/rdb/dml/export', { method: 'post' });
127127

128128
export default {
129129
getList,
@@ -142,5 +142,5 @@ export default {
142142
addTablePin,
143143
deleteTablePin,
144144
getDMLCount,
145-
exportResultTable
145+
// exportResultTable
146146
};

chat2db-client/src/utils/common.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,45 @@ export function formatParams(obj: { [key: string]: any }) {
1414
});
1515
return params.toString();
1616
}
17+
18+
export function downloadFile(url: string, params: any) {
19+
// 创建POST请求
20+
fetch(url, {
21+
method: 'POST',
22+
headers: {
23+
'Content-Type': 'application/json', // 或者根据服务端的要求设置其他的内容类型
24+
},
25+
body: JSON.stringify(params), // 将参数转换为JSON字符串
26+
})
27+
.then((response) => {
28+
// 从content-disposition头中获取文件名
29+
const contentDisposition = response.headers.get('content-disposition');
30+
const filename = contentDisposition ? decodeURIComponent(contentDisposition.split("''")[1]) : 'file.txt';
31+
32+
// 获取返回的Blob数据
33+
return response.blob().then((blob) => ({ blob, filename }));
34+
})
35+
.then(({ blob, filename }) => {
36+
// 创建一个代表Blob对象的URL
37+
const blobUrl = URL.createObjectURL(blob);
38+
39+
// 创建一个隐藏的 <a> 标签,并设置其 href 属性
40+
const a = document.createElement('a');
41+
a.style.display = 'none';
42+
a.href = blobUrl;
43+
44+
// 使用从响应头解析的文件名
45+
a.download = filename;
46+
47+
// 将 <a> 标签附加到 DOM,并触发点击事件
48+
document.body.appendChild(a);
49+
a.click();
50+
51+
// 清理:从 DOM 中移除 <a> 标签,并释放Blob URL
52+
document.body.removeChild(a);
53+
URL.revokeObjectURL(blobUrl);
54+
})
55+
.catch((error) => {
56+
console.error('下载文件失败:', error);
57+
});
58+
}

0 commit comments

Comments
 (0)