refactor downloading file
This commit is contained in:
@@ -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);
|
||||
|
||||
28
src/frontend/js/DownloadUtils.js
Normal file
28
src/frontend/js/DownloadUtils.js
Normal 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;
|
||||
Reference in New Issue
Block a user