Skip to content

Commit 0994775

Browse files
authored
Merge pull request #17 from scalefocus/fix/141_multiple_download_single_file
Multiple Download, download single file as it is
2 parents a0366ac + 931c37f commit 0994775

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/api/api.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,18 @@ export async function downloadObjectsAsZip({
314314
objectIds,
315315
}: {
316316
objectIds: string[];
317-
}): Promise<string> {
317+
}): Promise<{ href: string; disposition: string }> {
318318
const res = await axiosClient.post('/object/downloadZip', {
319319
ObjectIds: objectIds,
320320
},{
321321
responseType: 'blob',
322322
});
323323

324-
const data = URL.createObjectURL(res.data);
325-
return data;
326-
};
324+
const href = URL.createObjectURL(res.data);
325+
const disposition = res.headers['content-disposition'];
326+
327+
return { href, disposition };
328+
}
327329

328330
export const fetchTrashedIds = async ({
329331
pageParam,
@@ -346,7 +348,7 @@ export async function trashRestoreObjects({
346348
});
347349

348350
return response.data;
349-
};
351+
}
350352

351353
export async function trashDeletePermamnentObjects({
352354
objectIds,
@@ -358,10 +360,10 @@ export async function trashDeletePermamnentObjects({
358360
});
359361

360362
return response.data;
361-
};
363+
}
362364

363365
export async function emptyTrash(): Promise<boolean> {
364366
const response = await axiosClient.delete('emptytrash');
365367

366368
return response.data;
367-
};
369+
}

src/components/ImageGallery.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,22 @@ export const ImageGallery: React.FC = () => {
138138
downloadObjectMutation.mutate(
139139
{ objectIds: selectedImages },
140140
{
141-
onSuccess: (href) => {
142-
// create "a" HTML element with href to file & click
141+
onSuccess: ({ href, disposition }) => {
142+
// Extract filename from Content-Disposition header
143+
let filename = 'files.zip';
144+
if (disposition) {
145+
const match = disposition.match(
146+
/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
147+
);
148+
if (match && match[1])
149+
filename = match[1].replace(/['"]/g, '');
150+
}
151+
143152
const link = document.createElement('a');
144153
link.href = href;
145-
link.setAttribute('download', 'files.zip'); //or any other extension
154+
link.setAttribute('download', filename);
146155
document.body.appendChild(link);
147156
link.click();
148-
149-
// clean up "a" element & remove ObjectURL
150157
document.body.removeChild(link);
151158
URL.revokeObjectURL(href);
152159
},

0 commit comments

Comments
 (0)