diff --git a/server/static/js/import.js b/server/static/js/import.js new file mode 100644 index 0000000..d291018 --- /dev/null +++ b/server/static/js/import.js @@ -0,0 +1,38 @@ +let importButton = document.getElementById('import'); + +importButton.addEventListener('click', importData); + +async function importData(event) { + let fileInput = document.createElement('input'); + fileInput.type = 'file'; + fileInput.accept = '.json'; + + fileInput.onchange = event => { + const file = event.target.files[0]; + if (file) { + const reader = new FileReader(); + + reader.onload = function(e) { + try { + const jsonData = JSON.parse(e.target.result); + sendJsonData(jsonData); + } catch (error) { + alert("Ошибка при чтении файла JSON: " + error.message); + } + }; + reader.readAsText(file); + } + } + fileInput.click() +} + +async function sendJsonData(jsonData) { + await fetch(new URL('/import_db', 'http://localhost:8000').href, { + method: "POST", + headers: { + 'Accept': 'application/json', + "Content-type": "application/json; charset=UTF-8" + }, + body: JSON.stringify(jsonData) + }); +} \ No newline at end of file diff --git a/server/templates/index.html b/server/templates/index.html index 6ae181e..016e92b 100644 --- a/server/templates/index.html +++ b/server/templates/index.html @@ -12,12 +12,13 @@