Detect when browser receives file download

前端 未结 22 1691
陌清茗
陌清茗 2020-11-21 04:55

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,

22条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 05:46

    I wrote a simple JavaScript class that implements a technique similar to the one described in bulltorious answer. I hope it can be useful to someone here. The GitHub project is called response-monitor.js

    By default it uses spin.js as the waiting indicator but it also provides a set of callbacks for implementation of a custom indicator.

    JQuery is supported but not required.

    Notable features

    • Simple integration
    • No dependencies
    • JQuery plug-in (optional)
    • Spin.js Integration (optional)
    • Configurable callbacks for monitoring events
    • Handles multiple simultaneous requests
    • Server-side error detection
    • Timeout detection
    • Cross browser

    Example usage

    HTML

    
    
    
    
     
    
    Link 1 (Timeout: 30s)
    Link 2 (Timeout: 10s)
    
    

    Client (plain JavaScript)

    //registering multiple anchors at once
    var my_anchors = document.getElementsByClassName('my_anchors');
    ResponseMonitor.register(my_anchors); //clicking on the links initiates monitoring
    
    //registering a single form
    var my_form = document.getElementById('my_form');
    ResponseMonitor.register(my_form); //the submit event will be intercepted and monitored
    

    Client (JQuery)

    $('.my_anchors').ResponseMonitor();
    $('#my_form').ResponseMonitor({timeout: 20});
    

    Client with callbacks (JQuery)

    //when options are defined, the default spin.js integration is bypassed
    var options = {
        onRequest: function(token){
            $('#cookie').html(token);
            $('#outcome').html('');
            $('#duration').html(''); 
        },
        onMonitor: function(countdown){
            $('#duration').html(countdown); 
        },
        onResponse: function(status){
            $('#outcome').html(status==1?'success':'failure');
        },
        onTimeout: function(){
            $('#outcome').html('timeout');
        }
    };
    
    //monitor all anchors in the document
    $('a').ResponseMonitor(options);
    

    Server (PHP)

    $cookiePrefix = 'response-monitor'; //must match the one set on the client options
    $tokenValue = $_GET[$cookiePrefix];
    $cookieName = $cookiePrefix.'_'.$tokenValue; //ex: response-monitor_1419642741528
    
    //this value is passed to the client through the ResponseMonitor.onResponse callback
    $cookieValue = 1; //for ex, "1" can interpret as success and "0" as failure
    
    setcookie(
        $cookieName,
        $cookieValue,
        time()+300,            // expire in 5 minutes
        "/",
        $_SERVER["HTTP_HOST"],
        true,
        false
    );
    
    header('Content-Type: text/plain');
    header("Content-Disposition: attachment; filename=\"Response.txt\"");
    
    sleep(5); //simulate whatever delays the response
    print_r($_REQUEST); //dump the request in the text file
    

    For more examples check the examples folder on the repository.

提交回复
热议问题