繁体   English   中英

开始下载文件之前使用php显示消息

[英]Display message using php before starting file download

我正在使用此PHP代码开始下载,但想在启动下载之前/之后显示“ HTML”,但是以下代码导致空白页:

输出html的函数:

function writeMsg($msg)
{
    echo "<html><body><h2>$msg</h2></body></html>";
}

调用方式为:

if(file_exists ($fullpath)) {
//Start Download
ob_start();
writeMsg('Download Started');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$fname\"");
header("Content-Transfer-Encoding: binary");
header('Connection: close');
ob_end_flush();
exit();
}

这是不可能的,因为您将标头类型设置为application / octet-stream,这意味着它是一个二进制文件,必须使用应用程序打开。

相反,您可以显示文本,然后使用HTML或Javascript重定向用户

在HTML中,您可以使用以下内容

<META http-equiv="refresh" content="NUMBER_OF_SECONDS_HERE; URL=DOWNLOAD_LINK_HERE">

尝试这个。 如果不显示内联文本,则可能需要一个模式来显示加载文本。

HTML MARK UP 
<a href="#" id="d1">Download Now</a>

// Jquery
$(function(){
 $('#d1').click(function(){

   $(this).text('Download Started...');

   setTimeout(function(){

        window.location.replace('http://domain.com/download.php');

   },2000);

   return false;

 })
});

这只能在客户端使用javascript来完成,而我现在正在使用它: https : //github.com/johnculviner/jquery.fileDownload

jQuery File Download是一个跨服务器平台兼容的jQuery插件,它提供了Ajax一样的文件下载体验,而这通常是使用Web无法实现的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM