Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsing a file from window.postMessage() #403

Merged
merged 1 commit into from
Sep 24, 2023

Conversation

Williangalvani
Copy link
Collaborator

@Williangalvani Williangalvani commented Sep 24, 2023

the other side should do something like this:

!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>File Loader</title>
</head>
<body>
  <input type="file" id="fileInput">

  <script>
    const fileInput = document.getElementById('fileInput')

    fileInput.addEventListener('change', function(event) {
      const file = event.target.files[0]
      if (file) {
        const reader = new FileReader()

        reader.onload = function(e) {
          const arrayBuffer = e.target.result

          // Open the new page and keep a reference to it
          const newWindow = window.open('http://localhost:8080')

          // Wait a bit to ensure the new page is fully loaded
          setTimeout(() => {
            // Send the ArrayBuffer to the new window using postMessage
            newWindow.postMessage({ type: 'arrayBuffer', data: arrayBuffer }, 'http://localhost:8080')
          }, 2000)
        }

        reader.readAsArrayBuffer(file)
      }
    })
  </script>
</body>
</html>

This passes the whole ArrayBuffer through the message, which is not ideal, but it seems to work well enough.

@Williangalvani Williangalvani merged commit 5fcb435 into ArduPilot:master Sep 24, 2023
2 checks passed
@Williangalvani Williangalvani deleted the postmessage branch September 24, 2023 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant