【问题标题】:Display message using php before starting file download在开始文件下载之前使用 php 显示消息
【发布时间】:2013-09-10 15:44:09
【问题描述】:

我正在使用此 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();
}

【问题讨论】:

    标签: php file-io nginx http-headers


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

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

      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;
      
       })
      });
      

      【讨论】:

      • 还有一件事,我正在使用 X-Sendfile 标头来推送文件,而不会透露磁盘上的原始文件位置以防止直接下载 - 那么这是否适用?
      【解决方案3】:

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

      jQuery File Download 是一个跨服务器平台兼容的 jQuery 允许类似 Ajax 的文件下载体验的插件 通常无法使用网络。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-12
        • 2015-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多