LiLongLong 2 months ago
parent
commit
9095416a12
  1. 50
      src/pages/measure/components/MeasureDetail.tsx
  2. 19
      src/pages/system/Setting.tsx
  3. 1
      src/services/measure/type.ts
  4. 7
      src/utils/index.ts

50
src/pages/measure/components/MeasureDetail.tsx

@ -145,18 +145,18 @@ export default function MeasureDetail() {
}
const onDownloadData = async () => {
setIdList([])
let list = [...selectRows]
let Ids:number[] = []
list.map(item => {
Ids.push(item.id)
})
setIdList(Ids)
const response = await fetch(`/api/measurement-task/downloads/${Ids.join(',')}`);
const blob = await response.blob(); // 先获取 Blob
const arrayBuffer = await blob.arrayBuffer(); // 将 Blob 转换为 ArrayBuffer
exportFile(arrayBuffer, `坐标数据.xls`); // 传递 ArrayBuffer 给函数
}
// 1. 准备 id 列表
const Ids = selectRows.map(r => r.id);
// 2. 发起请求
const response = await fetch(`/api/measurement-data/downloads-zip/${Ids.join(',')}`);
// 3. 拿到 Blob
const blob = await response.blob();
// 4. 解析后端文件名
const contentDisp = response.headers.get('content-disposition');
const filename = parseFilename(contentDisp) || 'download.zip';
// 5. 触发下载
exportFile(blob, filename);
};
const [percent, setPercent] = useState(0)
@ -233,13 +233,29 @@ export default function MeasureDetail() {
getMeasurePoints(item)
}
function parseFilename(disposition: string | null): string | null {
if (!disposition) return null;
// 匹配 filename*=UTF-8''xxx 或 filename=xxx(带不带引号都能捕获)
const match = disposition.match(/filename\*?=(?:UTF-8'')?["']?([^"';]+)["']?/i);
return match ? decodeURIComponent(match[1]) : null;
}
//导出
const onDownloadRecord = async (item: DetailTable) => {
const response = await fetch(`/api/measurement-task/download/${item.id}`);
const blob = await response.blob(); // 先获取 Blob
const arrayBuffer = await blob.arrayBuffer(); // 将 Blob 转换为 ArrayBuffer
exportFile(arrayBuffer, `${item.name}.txt`); // 传递 ArrayBuffer 给函数
}
// 1. 发请求
const response = await fetch(`/api/measurement-data/download/${item.id}`);
// 2. 拿到 Blob
const blob = await response.blob();
// 3. 解析头里的文件名
const contentDisp = response.headers.get('content-disposition');
// fallback 到 item.name + .txt
const filename = parseFilename(contentDisp) || `${item.name}.txt`;
// 4. 触发下载
exportFile(blob, filename);
};
const getMeasurePoints = (recordItem:DetailTable) => {
getPointsById({id:recordItem.id}).then(res=>{

19
src/pages/system/Setting.tsx

@ -346,6 +346,13 @@ export default function Setting(){
>
<Switch defaultChecked onChange={onTodayNumberFlagChange} />
</Form.Item>
<Form.Item label="导出文件格式" name="downloadFileType" initialValue="txt">
<Select style={{ width: 120 }} onChange={onDownloadFileTypeChange} >
<Select.Option value="txt">txt</Select.Option>
<Select.Option value="ban">ban</Select.Option>
</Select>
</Form.Item>
</Form>
}
@ -367,6 +374,18 @@ export default function Setting(){
})
}
const onDownloadFileTypeChange = (value: string) => {
const params: StandbyMinutes = {
downloadFileType: value,
}
saveDeviceConfig(params).then(res=> {
message.success("保存成功");
setSaveLoading(false)
}).catch(e => {
setSaveLoading(false)
})
}
const onTodayNumberFlagChange = (value: boolean) => {
const params: StandbyMinutes = {
todayNumberFlag: value,

1
src/services/measure/type.ts

@ -132,4 +132,5 @@ export type trackItem = {
export type StandbyMinutes = {
standbyMinutes?: string | number
todayNumberFlag?: boolean
downloadFileType?: string
}

7
src/utils/index.ts

@ -87,17 +87,12 @@ export function findSymmetricPoint(px: number, py: number, ox = 0, oy = 0) {
}
export function exportFile(response: ArrayBuffer, fileName: string){
// 创建 Blob 对象
const blob = new Blob([response], { type: 'application/octet-stream' });
// 创建一个 URL 对象,将 Blob 对象转换为 URL
export function exportFile(blob: Blob, fileName: string) {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
// 模拟点击 <a> 元素来触发下载
a.click();
// 释放 URL 对象
window.URL.revokeObjectURL(url);
}

Loading…
Cancel
Save