Files
cpost-front/src/services/file-service/index.ts
azizziy c01e852a59 init
2025-05-20 17:02:10 +05:00

30 lines
854 B
TypeScript

class FileService {
download(file: File, name?: string) {
const link = document.createElement('a');
const fileUrl = URL.createObjectURL(file);
link.download = name || file.name;
link.href = fileUrl;
link.click();
URL.revokeObjectURL(fileUrl);
}
downloadByDisposition(disposition: string, blob: Blob) {
const fileUrl = URL.createObjectURL(blob);
const contentDisposition = disposition;
const filename = contentDisposition.split('filename=')[1] as string;
const downloadFileName = filename.slice(1, filename.length - 1);
const link = document.createElement('a');
link.download = downloadFileName;
link.href = fileUrl;
link.click();
URL.revokeObjectURL(fileUrl);
}
}
export const file_service = new FileService();