Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3344928
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:07:35+00:00 2026-05-18T01:07:35+00:00

I have a page that allows the user to download a dynamically-generated file. It

  • 0

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 "waiting" 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.

I’m requesting a hidden form, which POSTs 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 "load" event on the iframe, hoping that it will fire when the download is complete.

I return a "Content-Disposition: attachment" header with the file, which causes the browser to show the "Save" dialog. But the browser doesn’t fire a "load" event in the iframe.

One approach I tried is using a multi-part response. So it would send an empty HTML file, as well as the attached downloadable file.

For example:

Content-type: multipart/x-mixed-replace;boundary="abcde"

--abcde
Content-type: text/html

--abcde
Content-type: application/vnd.fdf
Content-Disposition: attachment; filename=foo.fdf

file-content
--abcde

This works in Firefox; it receives the empty HTML file, fires the "load" event, and then shows the "Save" dialog for the downloadable file. But it fails on Internet Explorer and Safari; Internet Explorer fires the "load" event, but it doesn’t download the file, and Safari downloads the file (with the wrong name and content-type) and doesn’t fire the "load" event.

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.

What should I do?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:07:36+00:00Added an answer on May 18, 2026 at 1:07 am

    One possible solution uses JavaScript on the client.

    The client algorithm:

    1. Generate a random unique token.
    2. Submit the download request, and include the token in a GET/POST field.
    3. Show the “waiting” indicator.
    4. Start a timer, and every second or so, look for a cookie named “fileDownloadToken” (or whatever you decide).
    5. If the cookie exists, and its value matches the token, hide the “waiting” indicator.

    The server algorithm:

    1. Look for the GET/POST field in the request.
    2. If it has a non-empty value, drop a cookie (e.g. “fileDownloadToken”), and set its value to the token’s value.

    Client source code (JavaScript):

    function getCookie( name ) {
      var parts = document.cookie.split(name + "=");
      if (parts.length == 2) return parts.pop().split(";").shift();
    }
    
    function expireCookie( cName ) {
        document.cookie = 
            encodeURIComponent(cName) + "=deleted; expires=" + new Date( 0 ).toUTCString();
    }
    
    function setCursor( docStyle, buttonStyle ) {
        document.getElementById( "doc" ).style.cursor = docStyle;
        document.getElementById( "button-id" ).style.cursor = buttonStyle;
    }
    
    function setFormToken() {
        var downloadToken = new Date().getTime();
        document.getElementById( "downloadToken" ).value = downloadToken;
        return downloadToken;
    }
    
    var downloadTimer;
    var attempts = 30;
    
    // Prevents double-submits by waiting for a cookie from the server.
    function blockResubmit() {
        var downloadToken = setFormToken();
        setCursor( "wait", "wait" );
    
        downloadTimer = window.setInterval( function() {
            var token = getCookie( "downloadToken" );
    
            if( (token == downloadToken) || (attempts == 0) ) {
                unblockSubmit();
            }
    
            attempts--;
        }, 1000 );
    }
    
    function unblockSubmit() {
      setCursor( "auto", "pointer" );
      window.clearInterval( downloadTimer );
      expireCookie( "downloadToken" );
      attempts = 30;
    }
    

    Example server code (PHP):

    $TOKEN = "downloadToken";
    
    // Sets a cookie so that when the download begins the browser can
    // unblock the submit button (thus helping to prevent multiple clicks).
    // The false parameter allows the cookie to be exposed to JavaScript.
    $this->setCookieToken( $TOKEN, $_GET[ $TOKEN ], false );
    
    $result = $this->sendFile();
    

    Where:

    public function setCookieToken(
        $cookieName, $cookieValue, $httpOnly = true, $secure = false ) {
    
        // See: http://stackoverflow.com/a/1459794/59087
        // See: http://shiflett.org/blog/2006/mar/server-name-versus-http-host
        // See: http://stackoverflow.com/a/3290474/59087
        setcookie(
            $cookieName,
            $cookieValue,
            2147483647,            // expires January 1, 2038
            "/",                   // your path
            $_SERVER["HTTP_HOST"], // your domain
            $secure,               // Use true over HTTPS
            $httpOnly              // Set true for $AUTH_COOKIE_NAME
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page that allows for used input, when the user inputs his/her
I have an ASP.NET (3.5) page that allows a user to upload an Excel
I have an online store. A products page that allows the user to view
I have a html page that allows users to submit a file. Below is
I have a page that has fields dynamically loaded via JQuery. It allows a
I have a page that allows a user to choose some things in a
I have an asp.net website that allows the user to download largish files -
I have a page that allows the user to add and remove text fields
I have created a page that allows a user to enter a start location
I have a web page that uses three controls to allow a user to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.