title: Detect when a browser receives a file download
body: <p>I have a page that allows the user to download a dynamically-generated file. It takes a long time to generate, so I'd like to show a &quot;waiting&quot; indicator. The problem is, I can't figure out how to detect when the browser has received the file so that I can hide the indicator.</p>
  <p>I'm requesting a hidden form, which <a href="https://en.wikipedia.org/wiki/POST_%28HTTP%29" rel="noreferrer">POSTs</a> to the server, and targets a hidden iframe for its results. This is, so I don't replace the entire browser window with the result. I listen for a &quot;load&quot; event on the iframe, hoping that it will fire when the download is complete.</p>
  <p>I return a &quot;<code>Content-Disposition: attachment</code>&quot; header with the file, which causes the browser to show the &quot;Save&quot; dialog. But the browser doesn't fire a &quot;load&quot; event in the iframe.</p>
  <p>One approach I tried is using a <code>multi-part</code> response. So it would send an empty HTML file, as well as the attached downloadable file.</p>
  <p>For example:</p>
  <pre><code>Content-type: multipart/x-mixed-replace;boundary=&quot;abcde&quot;
  
  --abcde
  Content-type: text/html
  
  --abcde
  Content-type: application/vnd.fdf
  Content-Disposition: attachment; filename=foo.fdf
  
  file-content
  --abcde
  </code></pre>
  <p>This works in Firefox; it receives the empty HTML file, fires the <em>&quot;load&quot;</em> event, and then shows the <em>&quot;Save&quot;</em> dialog for the downloadable file. But it fails on <a href="https://en.wikipedia.org/wiki/Internet_Explorer" rel="noreferrer">Internet Explorer</a> and <a href="https://en.wikipedia.org/wiki/Safari_%28web_browser%29" rel="noreferrer">Safari</a>; Internet Explorer fires the &quot;load&quot; event, but it doesn't download the file, and <em>Safari downloads</em> the file (with the wrong name and content-type) and doesn't fire the <em>&quot;load&quot;</em> event.</p>
  <p>A different approach might be to call to start the file creation, poll the server until it's ready, and then download the already-created file. But I'd rather avoid creating temporary files on the server.</p>
  <p>What should I do?</p>