add file download
This commit is contained in:
@@ -128,6 +128,8 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
||||
const [openWindow, setOpenWindow] = useState<boolean>(false);
|
||||
const [openWindowChildren, setOpenWindowChildren] = useState<ReactNode>(null);
|
||||
|
||||
const [isFileLoading, setIsFileLoading] = useState(false);
|
||||
|
||||
const t = useTranslations("Global");
|
||||
|
||||
// Определение колонок
|
||||
@@ -343,6 +345,7 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleDownload(row.original)}
|
||||
disabled={isFileLoading}
|
||||
className="px-3 py-1 bg-green-500 text-white rounded hover:bg-green-600 text-sm"
|
||||
>
|
||||
<IconFileOpen />
|
||||
@@ -373,8 +376,31 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
||||
console.log(`Просмотр файла: ${file.fileName}`);
|
||||
};
|
||||
|
||||
const handleDownload = (file: FileItem) => {
|
||||
console.log(`Скачать: ${file.fileName}`);
|
||||
const handleDownload = async (file: FileItem) => {
|
||||
setIsFileLoading(true);
|
||||
console.log(`download: ${file.id}`);
|
||||
try {
|
||||
const response = await fetch(`/api/download/${file.id}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`error: ${response.status}`);
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = file.fileName || `file-${file.id}`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
document.body.removeChild(a);
|
||||
|
||||
} catch (error) {
|
||||
toast.error('Не удалось скачать файл')
|
||||
} finally {
|
||||
setIsFileLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleProtect = (file: FileItem) => {
|
||||
|
||||
Reference in New Issue
Block a user