fix file download for search list

This commit is contained in:
smanylov
2026-03-10 10:59:25 +07:00
parent 83f644cb03
commit 04ff892e86
+12 -5
View File
@@ -9,6 +9,7 @@ import { AllowedExtensions } from '@/app/ui/search/section-search-file';
import { FileTypeIcon } from '@/app/components/FileTypeIcon'; import { FileTypeIcon } from '@/app/components/FileTypeIcon';
import { formatDate, formatDateTime } from '@/app/lib/formatDate'; import { formatDate, formatDateTime } from '@/app/lib/formatDate';
import { FileInfo } from './file-search-panel'; import { FileInfo } from './file-search-panel';
import { downloadFile } from '@/app/actions/fileEntity';
export function SearchedUserFilesList({ list, allowedExtensions }: { list: FileInfo[], allowedExtensions: AllowedExtensions }) { export function SearchedUserFilesList({ list, allowedExtensions }: { list: FileInfo[], allowedExtensions: AllowedExtensions }) {
const [isFileLoading, setIsFileLoading] = useState(false); const [isFileLoading, setIsFileLoading] = useState(false);
@@ -20,17 +21,23 @@ export function SearchedUserFilesList({ list, allowedExtensions }: { list: FileI
setIsFileLoading(true); setIsFileLoading(true);
try { try {
const response = await fetch(`/api/download/${fileId}`); const result = await downloadFile(
fileId,
fileName || `file-${fileId}`
);
if (!response.ok) { const byteCharacters = atob(result.data);
throw new Error(`error: ${response.status}`); const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i)
} }
const blob = await response.blob(); const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], { type: result.contentType });
const url = window.URL.createObjectURL(blob); const url = window.URL.createObjectURL(blob);
const a = document.createElement('a'); const a = document.createElement('a');
a.href = url; a.href = url;
a.download = fileName || `file-${fileId}`; a.download = result.fileName;
document.body.appendChild(a); document.body.appendChild(a);
a.click(); a.click();
window.URL.revokeObjectURL(url); window.URL.revokeObjectURL(url);