File size: 842 Bytes
79278ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { GenericIdentityFn, IQueryErrorResponse } from "../api/query";

import { AxiosResponseHeaders } from "axios";

export const getContentFilename = (headers?: AxiosResponseHeaders) => {
  const fileNameHeader = headers?.get("content-disposition")?.toString();
  const regex = /filename="([^"]+)"/;
  const match = fileNameHeader?.match(regex);
  return match?.[1];
};

export const downloadFile = (
  response: IQueryErrorResponse | GenericIdentityFn<Blob>,
  filenameparam?: string
) => {
  if ("data" in response && response.data) {
    const url = URL.createObjectURL(response.data);
    const link = document.createElement("a");
    link.href = url;
    link.download = `${filenameparam ?? "file.pdf"}`;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    URL.revokeObjectURL(url);
  }
};