Massive delay between closing of <input type="file"> Select File Dialog and onchange event. Browser kind of freezing [duplicate]
Asked Answered
F

1

12

Sometimes I encounter a very strange behavior of the browser's native Select File Dialog.

I have a <input type="file" onchange="console.log(event.target.files)" /> element to upload a single file.

Usually the onchange event is triggered instantly (respectively after some milliseconds) after selecting a file in the Select File Dialog.

But sometimes the browser kind of freezes and it takes up to 10 seconds until the onchange event is called.

One thing I've noticed: If I have a network drive in my Windows Explorer Quick Access toolbar that is not reachable (because I'm not connected with VPN), this massive delay problem occurs much more often (although I do select a file on my Desktop that has nothing to do with this network drive).

Same in all major browsers (Chrome, Edge, Firefox), so it probably has something to do with the Windows Operating System.

Does somebody else facing this problem?

Minimal reproducible example:

<html>
  <head>
    <meta charset="UTF-8" />
    <script type="text/javascript">
      let timestamp;
      function onClick() {
        window.addEventListener('focus', fileDialogClosed);
      }
      function fileDialogClosed() {
        document.getElementById('result').innerHTML =
          'File Dialog closed.<br />';
        timestamp = new Date().getTime();
        window.removeEventListener('focus', fileDialogClosed);
      }
      function onChange(file) {
        let delay = new Date().getTime() - timestamp;
        document.getElementById('result').innerHTML +=
          'onchange event called with delay of <strong>' +
          delay +
          'ms</strong>';
        document.body.querySelector('input').value = null;
      }
    </script>
  </head>
  <body>
    <h1>File Input</h1>
    <p>
      Little demo to show/measure delay between closed file input dialog and
      call of onchange event handler.
    </p>
    <input
      type="file"
      onclick="onClick()"
      onchange="onChange(event.target.files[0])"
    /><br /><br />
    <div id="result"></div>
  </body>
</html>

Massive Delay between seleting a file and the triggering of onchange event

Fike answered 9/3, 2022 at 22:43 Comment(8)
You are required to post a minimal reproducible example here, within your question, and not a link to any other site.Aggregate
@Aggregate I've put the minimal reproducible example directly into my post.Fike
I confirm experiencing the same problem, using Firefox 99.0.1 (64-bits)Contradistinguish
Same issue using Vivaldi Vivaldi 5.5.2805.44 stable (Chromium-based)Enlace
Same problem with both Chrome Version 112.0.5615.139 (Official Build) (64-bit) and Firefox Version 112.0.2 (64-bit)Chinese
Does this answer your question? Browser freezing after selecting file in input fieldMccluskey
Could you try to disable or delete all mapped and network drives in your Windows File Explorer and see if that makes a difference ? When you click the File Input it contacts the Operating System to see what files it can chose from. If some of the directories in your file system are mapped or network drives it takes time to "wake them up" or negotiate a connection to them if they have been sitting idle for a while.Renvoi
Not having this issue on M2 MBP with Chrome Version 123.0.6312.107 (Official Build) (arm64). Getting a delay of not more than 300msTautologize
P
0

The code is running perfectly fine and in my case I got 30-90 ms by trying to upload something. Overall it's not a problem of the Windows, but more because of transfer speed to the browser because of such things like SATA SSD/HDD slow speed or, as you have already mentioned, a network drive speed in the Windows Explorer Quick Access toolbar.

Pippy answered 8/8 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.