Files
factorio-clusterio/packages/web_ui/src/util/save_file.ts
T
Cooldude2606andDanielv123 0e6d33aa82 Add button to download ctl config (#743)
* Move saveJson to util file

* Add button to download ctl config

* Add icon and tooltip to describe what the button does

---------

Co-authored-by: Danielv123 <danielv@danielv.no>
2025-04-08 20:34:50 +01:00

26 lines
865 B
TypeScript

/**
* Downloads a file to the users PC, should only be used following direct user input.
* @param name Name of the file to save
* @param blob Content to save
*/
export function saveFile(name: string, blob: Blob) {
// This is the most common and best supported method, for full support we should consider using npm:file-saver
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = name;
a.addEventListener("click", (e) => {
setTimeout(() => URL.revokeObjectURL(a.href), 30 * 1000);
});
a.click();
};
/**
* Downloads a json file to the users PC
* @param name Name of the file to save, should include .json
* @param json The object to save as a formated json string
*/
export function saveJson(name: string, json: object) {
saveFile(name, new Blob([JSON.stringify(json, null, 2)], { type: "application/json" }));
}