Show a “Please Wait” Message or a Progress Bar while Files Download

江枫思渺然 提交于 2019-12-20 05:52:21

问题


I use the following WordPress admin notice to prompt users to download some files. I would like to include either a progress bar or at least a "Downloading - Please wait" message while files are downloading.

Any ideas?

I've tried several jQuery solutions but could get nothing to work. I'm a total noob when it comes to jQuery.

/* Ask user to download GeoIP database files. */
add_action( 'admin_notices', 'lsmi_dl_admin_notice' );
add_action( 'network_admin_notices', 'lsmi_dl_admin_notice' ); // also show message on multisite
function lsmi_dl_admin_notice() {
    $dir = dirname( __FILE__ );
    $localfilev4 = $dir . '/data/GeoIPv4.dat';
    $localfilev6 = $dir . '/data/GeoIPv6.dat';
    $ctx = stream_context_create( array( 'http' => array( 'timeout' => 120 ) ) ); 
    if ( !file_exists( $localfilev4 ) ) {
        if ( current_user_can( 'install_plugins' ) ) {
            echo
            '<div class="notice notice-warning is-dismissible"><p>Notice: This plugin uses Maxmind Geolite databases for better accuracy. Click the download button to install now.
            <form action="" method="get">
            <input type="submit" class="button" name="download" value="download" />
            </div>';
            if($_GET){
                if(isset($_GET['download'])){
                    $newfilev4 = file_get_contents( "https://sourceforge.net/projects/geoipupdate/files/GeoIPv4.dat/download", 0, $ctx );
                    file_put_contents( $dir . '/data/GeoIPv4.dat', $newfilev4 );
                    if ( !file_exists( $localfilev6 ) ) {
                        $newfilev6 = file_get_contents( "https://sourceforge.net/projects/geoipupdate/files/GeoIPv6.dat/download", 0, $ctx );
                        file_put_contents( $dir . '/data/GeoIPv6.dat', $newfilev6 );
                    }
                }
                echo '<meta http-equiv="refresh" content="0">';
            }
        }
    }
}

回答1:


Try giving your button an ID like so:

<input type="submit" class="button" name="download" value="download" id="download" />

Also give your div an id like so:

<div class="notice notice-warning is-dismissible" id="download-div">

Then we can use a basic jQuery onClick function and change the 's innerhtml like so:

    $("#download").click(
         function () {
             $('#download-div').html("Please wait...");
         }            
     );
 });

Hopefully this helps :)



来源:https://stackoverflow.com/questions/35789436/show-a-please-wait-message-or-a-progress-bar-while-files-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!