【问题标题】:I can't make a file dialog pop up我无法弹出文件对话框
【发布时间】:2021-10-26 22:33:56
【问题描述】:

这是我的代码。我在 godot 中使用 JavaScript.eval() 运行它是我的代码或游戏引擎的问题吗?

var input = document.createElement("input");
input.type = "file";
input.id = "fileImporter";
document.body.appendChild(input);
input.click();

【问题讨论】:

  • 代码通过网络浏览器加载时运行良好,我建议按照您的建议使用游戏引擎运行它。
  • 可能是安全限制。见here
  • 在这里工作正常
  • 感谢您的帮助。我会将此报告为错误。

标签: javascript godot


【解决方案1】:

根据您计划支持的浏览器,您可以考虑使用showOpenFilePicker API

示例: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());

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    相关资源
    最近更新 更多