1

This is my code. I am running it in godot with JavaScript.eval() is it a problem with my code or the game engine?

var input = document.createElement("input");
input.type = "file";
input.id = "fileImporter";
document.body.appendChild(input);
input.click();
vanowm
  • 9,466
  • 2
  • 21
  • 37

1 Answers1

0

Depending on which browsers you are planning to support, you could consider using the showOpenFilePicker API.

Example: https://rightful-serious-cost.glitch.me/

<textarea></textarea>
<button>Open file</button>
async function handleOpenFile() {
  const [fileHandle] = await window.showOpenFilePicker({
    types: [
      {
        description: 'JSON document',
        accept: {
          'text/*': ['.json'],
        },
      },
    ],
    multiple: false,
  });
  const file = await fileHandle.getFile();
  const contents = await file.text();

  console.log(contents);
  document.querySelector('textarea').textContent = contents;
}

document.querySelector('button').addEventListener('click', () => handleOpenFile());

Source code: https://glitch.com/edit/#!/rightful-serious-cost?path=index.html%3A13%3A6

Soc
  • 7,425
  • 4
  • 13
  • 30