在浏览器中查看 PDF,而不是下载


View PDF in browser rather than download

本网站上有会议,每次会议都附有显示会议记录的PDF。下一次会议会动态收集此附加的 PDF,并允许下载前几分钟。这一切都完美运行,但是,在前几分钟单击链接时,它总是打开一个另存为框。我想知道如何让它只显示在浏览器中(假设用户启用了 PDF 查看器)?

这是控制器的功能:

public function current_pdf($id = null, $pdf = null) {
$this->loadModel('Document');
$document = $this->Document->find('list', array(
    'fields' => array('Document.dir', 'Document.url')
        ));
$request_pdf = $this->Meeting->read(null, $id);
if ($pdf == 'current') {
  $file = $document[$request_pdf['Meeting']['minutes']];
  if (!empty($file)) {
    header("Content-Disposition: attachment; filename=" . urlencode($file));
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");
    header("Content-Length: " . filesize(WWW_ROOT . 'files/document/url/' . $request_pdf['Meeting']['minutes'] . '/' . $file));
    flush(); // this doesn't really matter.
    $fp = fopen(WWW_ROOT . 'files/document/url/' . $request_pdf['Meeting']['minutes'] . '/' . $file, "r");
    while (!feof($fp)) {
      echo fread($fp, 65536);
      flush(); // this is essential for large downloads
    }
    fclose($fp);
    //echo WWW_ROOT.'files/document/url/pdf/'.$file; die;
    //readfile(WWW_ROOT.'files/document/url/pdf/'.$file);
    exit;
  }
  $this->redirect(array('controller' => 'meetings', 'action' => 'view/' . $id));
} else {
  $file = $request_pdf['Meeting']['current_minutes'];
  if (!empty($file)) {
    //echo filesize(WWW_ROOT.'files/document/url/pdf/'.$file); die;
    /* header('Content-Description: File Transfer');
      header('Content-Type: application/octet-stream');
      header("Content-Type: application/force-download");
      header('Content-Disposition: attachment; filename=' . urlencode(basename($file)));
      // header('Content-Transfer-Encoding: binary');
      header('Expires: 0');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
      header('Content-Length: ' . filesize($file));
      ob_clean();
      flush(); */
    header("Content-Disposition: attachment; filename=" . urlencode($file));
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");
    header("Content-Length: " . filesize(WWW_ROOT . 'files/document/url/pdf/' . $file));
    flush(); // this doesn't really matter.
    $fp = fopen(WWW_ROOT . 'files/document/url/pdf/' . $file, "r");
    while (!feof($fp)) {
      echo fread($fp, 65536);
      flush(); // this is essential for large downloads
    }
    fclose($fp);
    //echo WWW_ROOT.'files/document/url/pdf/'.$file; die;
    //readfile(WWW_ROOT.'files/document/url/pdf/'.$file);
    exit;
  }
  $this->redirect(array('controller' => 'meetings', 'action' => 'view/' . $id));
}

}

这是视图中的链接:

<?php echo $this->Html->link(__('Download Previous Minutes'), array('controller' => 'meetings', 'action' => 'current_pdf', $download, 'current')); ?>

我尝试删除标题("内容类型...")代码行并将header("Content-Disposition: attachment; filename=" . urlencode($file));中的附件更改为内联。这将停止另存为弹出窗口,但在浏览器中显示大量虚假代码。

任何帮助将不胜感激,因为我目前非常困惑。

我相信

这就像在代码中删除这一行一样简单:

header("Content-Disposition: attachment; filename=" . urlencode($file));

此行会将文件作为下载服务器化,而不会在浏览器中显示内容。

我希望这有所帮助!