feat: Add CSV download functionality for filtered dataset entries

This commit is contained in:
averel10
2026-03-20 07:37:28 +01:00
parent 7d58abd30f
commit 3aee6c10a8
3 changed files with 128 additions and 1 deletions

View File

@@ -44,3 +44,42 @@ export async function downloadFilteredEntriesAsZip(
throw error;
}
}
export async function downloadFilteredEntriesAsCsv(
datasetId: number,
filters: FilterParams
) {
try {
const response = await fetch('/api/download-metadata-csv', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
datasetId,
filters,
}),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to download CSV');
}
// Get the CSV blob from response
const blob = await response.blob();
// Trigger download
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `dataset-${datasetId}-metadata-${new Date().toISOString().split('T')[0]}.csv`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} catch (error) {
console.error('Error downloading CSV:', error);
throw error;
}
}