fix(client): fix broken 'download' function on FailedOCR page
This commit is contained in:
parent
a6d9446d1f
commit
dd8555b088
|
|
@ -38,7 +38,7 @@ import {
|
||||||
} from '@mui/icons-material';
|
} from '@mui/icons-material';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { useAuth } from '../../contexts/AuthContext';
|
import { useAuth } from '../../contexts/AuthContext';
|
||||||
import api from '../../services/api';
|
import api, { documentService } from '../../services/api';
|
||||||
|
|
||||||
interface Document {
|
interface Document {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
@ -377,9 +377,12 @@ const RecentDocuments: React.FC<RecentDocumentsProps> = ({ documents = [] }) =>
|
||||||
</IconButton>
|
</IconButton>
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
onClick={() => {
|
onClick={async () => {
|
||||||
const downloadUrl = `/api/documents/${doc.id}/download`;
|
try {
|
||||||
window.open(downloadUrl, '_blank');
|
await documentService.downloadFile(doc.id, doc.original_filename || doc.filename);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Download failed:', error);
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<DownloadIcon fontSize="small" />
|
<DownloadIcon fontSize="small" />
|
||||||
|
|
|
||||||
|
|
@ -473,7 +473,13 @@ const FailedOcrPage: React.FC = () => {
|
||||||
<Tooltip title="Download Document">
|
<Tooltip title="Download Document">
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
onClick={() => window.open(`/api/documents/${document.id}/download`, '_blank')}
|
onClick={async () => {
|
||||||
|
try {
|
||||||
|
await documentService.downloadFile(document.id, document.original_filename || document.filename);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Download failed:', error);
|
||||||
|
}
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<DownloadIcon />
|
<DownloadIcon />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
|
|
@ -760,7 +766,13 @@ const FailedOcrPage: React.FC = () => {
|
||||||
<Tooltip title="Download Document">
|
<Tooltip title="Download Document">
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
onClick={() => window.open(`/api/documents/${doc.id}/download`, '_blank')}
|
onClick={async () => {
|
||||||
|
try {
|
||||||
|
await documentService.downloadFile(doc.id, doc.original_filename || doc.filename);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Download failed:', error);
|
||||||
|
}
|
||||||
|
}}
|
||||||
sx={{ color: theme.palette.secondary.main }}
|
sx={{ color: theme.palette.secondary.main }}
|
||||||
>
|
>
|
||||||
<DownloadIcon />
|
<DownloadIcon />
|
||||||
|
|
|
||||||
|
|
@ -151,6 +151,28 @@ export const documentService = {
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
downloadFile: async (id: string, filename?: string) => {
|
||||||
|
try {
|
||||||
|
const response = await api.get(`/documents/${id}/download`, {
|
||||||
|
responseType: 'blob',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create blob URL and trigger download
|
||||||
|
const blob = new Blob([response.data], { type: response.headers['content-type'] });
|
||||||
|
const url = window.URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = url;
|
||||||
|
link.download = filename || `document-${id}`;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
window.URL.revokeObjectURL(url);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Download failed:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
getOcrText: (id: string) => {
|
getOcrText: (id: string) => {
|
||||||
return api.get<OcrResponse>(`/documents/${id}/ocr`)
|
return api.get<OcrResponse>(`/documents/${id}/ocr`)
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue