From 80f61cc5216fe020f82fc303317e4098acb5d21d Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 4 May 2016 14:27:59 +0200 Subject: AJAX upload file --- js/savegame.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'js') diff --git a/js/savegame.js b/js/savegame.js index 4a08f91..d5a9ba1 100644 --- a/js/savegame.js +++ b/js/savegame.js @@ -24,7 +24,9 @@ function loadSave() { } function downloadSave() { - window.open("craftmine.php?op=downSave&filename="+getCheckedSave(), "_blank"); + var filename = getCheckedSave(); + if(filename == -1) downGame(); + else window.open("craftmine.php?op=downSave&filename="+getCheckedSave(), "_blank"); } function deleteSave() { @@ -40,3 +42,18 @@ function saveGame() { function downGame() { window.open("craftmine.php?op=downSave", "_blank"); } + +function uploadSave() { + var selectedFile = document.getElementById("selectedFile"); + if(selectedFile.files[0].size > 2000000) { + showError("File is too big."); + return; + } + + var form = new FormData(); // Doesn't work with IE < 10 (and Opera Mini), but, as always, who cares? + form.append("savefile", selectedFile.files[0]); + + sendRequest("upload.php", form, function(ret) { + console.log(ret); + }, true); +} -- cgit v1.2.3-70-g09d2 From ec7771705d62d09fcd142c87f4941604f31c1d76 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 4 May 2016 14:32:49 +0200 Subject: Don't send content-type when uploading file + update saves list when loading page --- js/craftmine.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'js') diff --git a/js/craftmine.js b/js/craftmine.js index 92fa45a..4e6490f 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -5,10 +5,10 @@ data = { level: 1 } -function sendRequest(url, params, callback) { +function sendRequest(url, params, callback, isFile) { var xhr = new XMLHttpRequest(); xhr.open("POST", url); - xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + if(!isFile) xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") { var data = JSON.parse(xhr.responseText); @@ -63,5 +63,6 @@ function updateMine() { function init() { initCraftMine(); changeTab(); + listSaves(); window.setInterval(updateMine, 1000); } -- cgit v1.2.3-70-g09d2