douju2474 2013-11-05 23:05
浏览 71

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

There are meetings on this website and each meeting has a PDF attached showing the meeting minutes. The next meeting that takes place dynamically collects this attached PDF and allows for a download as the previous minutes. This is all working perfectly, however, when clicking the link for the previous minutes it always opens up a save as box. I was wondering how to get this to just display in the browser (assuming the user has a PDF viewer enabled)?

Here is the function from the controller:

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

}

And here is the link in the view:

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

I have tried removing the header("Content-Type...") lines of code and changing attachment in header("Content-Disposition: attachment; filename=" . urlencode($file)); to inline. This stopped the save as pop-up but displayed a load of spurious code in the browser instead.

Any help would be greatly appreciated as I'm currently very confused.

  • 写回答

1条回答 默认 最新

  • dtvnbe1428 2013-11-06 03:13
    关注

    I believe this is as simple as removing this line in your code:

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

    This line will server up the file as a download, and not display the content in the browser.

    I hope that helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口