Skip to content

Instantly share code, notes, and snippets.

@vitaliisili
Created November 17, 2024 00:23
Show Gist options
  • Save vitaliisili/1894449447fd45977a6c34d8913d9a8e to your computer and use it in GitHub Desktop.
Save vitaliisili/1894449447fd45977a6c34d8913d9a8e to your computer and use it in GitHub Desktop.
Save javascript data in file
const saveDataAsFile = (data: any, filename: string) => {
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
// Clean up
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment