24 lines
616 B
TypeScript
24 lines
616 B
TypeScript
import { ResponseHandler } from "./RepsonseHandler";
|
|
|
|
export interface DownloadMetadata {
|
|
name?: string;
|
|
type?: string;
|
|
}
|
|
|
|
const DownloadFileHandler: ResponseHandler<DownloadMetadata> = (res, next, extra) => {
|
|
if (res.status === 404) {
|
|
console.log("Nothing to download");
|
|
return res.json().then(next);
|
|
}
|
|
return res.blob()
|
|
.then(data => {
|
|
let a = document.createElement("a");
|
|
a.href = window.URL.createObjectURL(data);
|
|
a.download = `${extra?.name || "file"}.${extra?.type || "txt"}`;
|
|
a.click();
|
|
next()
|
|
})
|
|
.catch(next);
|
|
}
|
|
|
|
export default DownloadFileHandler; |