mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-03-21 09:06:33 +03:00
31 lines
799 B
TypeScript
31 lines
799 B
TypeScript
export const saveFile = (
|
|
json: unknown,
|
|
filename: string,
|
|
extension: string
|
|
): void => {
|
|
try {
|
|
const blob = new Blob([JSON.stringify(json, null, 2)], {
|
|
type: 'application/json'
|
|
});
|
|
const url = URL.createObjectURL(blob);
|
|
const anchor = document.createElement('a');
|
|
anchor.href = url;
|
|
anchor.download = `emsesp_${filename}${extension}`;
|
|
|
|
// Trigger download
|
|
document.body.appendChild(anchor);
|
|
anchor.click();
|
|
document.body.removeChild(anchor);
|
|
|
|
// Delay revocation to ensure download starts properly
|
|
setTimeout(() => {
|
|
URL.revokeObjectURL(url);
|
|
}, 100);
|
|
} catch (error) {
|
|
console.error('Failed to save file:', error);
|
|
throw new Error(`Unable to save file: ${filename}${extension}`, {
|
|
cause: error
|
|
});
|
|
}
|
|
};
|