Skip to content

Latest commit

 

History

History
47 lines (37 loc) · 951 Bytes

File metadata and controls

47 lines (37 loc) · 951 Bytes

File

downloadFile

const checkStatus = (response: Response) => {
  if (response.ok) return response;
  throw new Error(response.status.toString());
};

const toBlob = (response: Response) => response.blob();

const saveFile = (fileName: string) => (blob: Blob) => {
  const anchor = document.createElement('a');
  const href = URL.createObjectURL(blob);

  anchor.setAttribute('download', fileName);
  anchor.setAttribute('href', href);
  anchor.click();
  anchor.remove();
  URL.revokeObjectURL(href);
};

const downloadFile = (url: string, fileName: string) => (
  fetch(url, {
    method: 'get',
    mode: 'no-cors',
    referrerPolicy: 'no-referrer',
  })
    .then(checkStatus)
    .then(toBlob)
    .then(saveFile(fileName))
);

checkIsFileExist

const checkIsFileExist = (file) => {
  const xhr = new XMLHttpRequest();

  xhr.open('HEAD', file, false);
  xhr.send();

  return xhr.status !== 404;
}