Using HTML, CSS, and JS I have written a word chain game that uses JSON files to store game data for the many rounds of a single game. I wish to make it fairly user-friendly, so I want to make it possible to drag and drop the file onto the game window. I get a CORS error when running the HTML locally as one expects (it worked with --disable-web-security
set). However, I get an error when running the game from the server:
Not allowed to load local resource: file:///C:/fakepath/<filename>.json
The HTML is very simple:
<input type="file" name="gameData" id="gameData"><button id="loadGame">Load Game</button><br><br>
The JS is the problem, I’m sure:
$("#loadGame").on("click", function() { // Load the game file, however it needs to be in the game directory to work!
var filePath = $("#gameData").val().replace(/^.*[\/]/, '');
// var filePath = $("#gameData").val(); // Version for online only game, maybe....
consoleWrite(`File "${filePath}" loaded from game directory.`);
$.getJSON(filePath, function(data) {
round1 = data.round1;
consoleWrite(`Round 1: ${round1}`);
round2 = data.round2;
consoleWrite(`Round 2: ${round2}`);
round3 = data.round3;
consoleWrite(`Round 3: ${round3}`);
round4 = data.round4;
consoleWrite(`Round 4: ${round4}`);
extraRound1 = data.extraRound1;
consoleWrite(`Extra 1: ${extraRound1}`);
extraRound2 = data.extraRound2;
consoleWrite(`Extra 2: ${extraRound2}`);
extraRound3 = data.extraRound3;
consoleWrite(`Extra 3: ${extraRound3}`);
bonusRoundA = data.bonusRoundA;
consoleWrite(`Bonus A: ${bonusRoundA}`);
bonusRoundB = data.bonusRoundB;
consoleWrite(`Bonus B: ${bonusRoundB}`);
bonusRoundC = data.bonusRoundC;
consoleWrite(`Bonus C: ${bonusRoundC}`);
});
consoleWrite(`Game loaded.`);
});
What must I change to allow the game to parse the file? Is there any way to read the file’s contents locally without uploading it to a server? I’d like it to be able to work locally and hosted on a server. The “less-user-friendly” workaround I thought of is to have the user copy and paste the contents of the file into a textbox; I’ve done stuff like that before, and while it works I’d rather keep with a simple file upload option. Any ideas?