我在我的项目中制作了一个下载表单,但问题是当我下载文件并尝试打开它时,Zend 渲染器正在向其中添加我的布局 html 代码......我读到我必须禁用渲染器和布局。但问题是我必须在我自己的助手中执行此操作,而不是在控制器文件中,因为我需要在该助手文件中下载。我的下载功能是这样的:
<?php
class Zend_View_Helper_EditArticles extends Zend_View_Helper_Abstract
{
public function EditArticles()
{
//some code here, getting data from db table
//and now the download
if (isset($_POST['downloadarticle' . $i])) {
//this is probably bad and its not working as it should
//(?)Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
//(?)Zend_Controller_Action_HelperBroker::getStaticHelper('layout')->disableLayout();
$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/articles/';
$file = $articles->GetArticleToDownload($_POST['art_id' . $i]);
$name = $file['name'];
$path = $file['path'];
$getfile = str_replace('//', '/', $targetPath) . $path . '.pdf';
$size = $file['size'];
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$name.pdf");
header("Content-length: $size");
header('Content-Transfer-Encoding: binary');
readfile($getfile);
break;
}
}
echo $this->view->partial('/index/index.phtml','EditArticles');
当我下载 PDF 时,Adobe Reader 无法打开它(当我下载其他文件时,它们也无法打开)。我用记事本打开它们,在 PDF 内容之前有很多 HTML 布局代码......我做错了什么?
在 Adobe Reader 中,我收到以下消息:
Adobe Reader 无法打开“filename.pdf”,因为它不是受支持的文件类型,或者因为文件已损坏,例如,它作为电子邮件附件发送且未正确解码)。