1

该网站上有会议,每次会议都附有显示会议记录的 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')); ?>

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

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

4

1 回答 1

1

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

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

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

我希望这会有所帮助!

于 2013-11-06T03:13:03.803 回答