【问题标题】:Problems With Forcing Download of a PDF File强制下载 PDF 文件的问题
【发布时间】:2011-01-23 23:12:56
【问题描述】:

我有以下强制下载文件的功能:

static public function download($file, $options=array()) {
    $content = (isset($options['content'])) ? $options['content'] : '';
    $contentType = (isset($options['contentType'])) ? $options['contentType'] : '';
    header('Cache-Control: public');
    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename='.File::filename($file));
    header('Content-Type: '.$contentType);
    header('Content-Transfer-Encoding: binary');
    if ($content!='') {
        echo $content;
    } else {
        readfile($file);
    }
}

我发送一个 PDF 文件和 contentType = "application/pdf"。 问题是当我尝试打开下载的 PDF 文件时,它显示“打开此文档时出错。文件可能已损坏”。 很奇怪,因为我可以打开原始文件,它们看起来完全一样(文件名、大小等)

【问题讨论】:

  • 运行前有输出吗?你能把exit放在最后确保没有其他输出发送吗?
  • 谢谢亚历克斯,就是这样!我在附和一些东西,这就是问题所在......如果你愿意,你可以发表评论作为答案,这样我就可以将其投票为选定的人

标签: php pdf force-download


【解决方案1】:

确保在运行此函数之前没有输出,并且为了更好地衡量,请在此函数末尾使用exit 构造:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 2014-11-19
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多