refactor downloading file

This commit is contained in:
liamcottle
2025-01-01 20:55:56 +13:00
parent 372e61ed7c
commit 82c67bb71c
2 changed files with 36 additions and 8 deletions

View File

@@ -84,6 +84,7 @@ import ElectronUtils from "../../js/ElectronUtils";
import Interface from "./Interface.vue";
import Utils from "../../js/Utils";
import ImportInterfacesModal from "./ImportInterfacesModal.vue";
import DownloadUtils from "../../js/DownloadUtils";
export default {
name: 'InterfacesPage',
@@ -250,17 +251,16 @@ export default {
},
async exportInterfaces() {
try {
// fetch exported interfaces
const response = await window.axios.get('/api/v1/reticulum/interfaces/export', {
responseType: 'blob'
});
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'reticulum_interfaces');
document.body.appendChild(link);
link.click();
link.remove();
// download file to browser
const blob = new Blob([response.data]);
DownloadUtils.downloadFile("meshchat_interfaces", blob);
} catch(e) {
DialogUtils.alert("Failed to export interfaces");
console.error(e);

View File

@@ -0,0 +1,28 @@
class DownloadUtils {
static downloadFile(filename, blob) {
// create object url for blob
const objectUrl = URL.createObjectURL(blob);
// create hidden link element to download blob
const link = document.createElement('a');
link.href = objectUrl;
link.download = filename;
link.style.display = "none";
document.body.append(link);
// click link to download file in browser
link.click();
// link element is no longer needed
link.remove();
// revoke object url to clear memory
setTimeout(() => URL.revokeObjectURL(objectUrl), 10000);
}
}
export default DownloadUtils;