正在下载没有扩展名的文件?
File is downloading without extension?
我正在尝试下载 PDF 文件,但是当我单击下载按钮时,它下载的文件没有扩展名。那是我下载的时候没有PDF扩展名!
动作:
public function executeDownload(sfWebRequest $request) {
$this->ebook_id = $request->getParameter('ebook', $this->default_ebook);
$this->ebook = $this->ebooks[$this->ebook_id];
if (!is_array($this->ebook))
return;
// Allow user to download the ebook (and make sure there is no other output)
$this->setLayout(false);
sfConfig::set('sf_web_debug', false);
$content = sfConfig::get('sf_root_dir').'/web'.$this->ebook['ebook'];
//echo "<p>PDF: $content</p>"; var_export(filesize($content));exit;
// Check if the file exists
$this->forward404Unless(file_exists($content));
// Record the download for this eBook
$c = new Criteria();
$c->add(EbookDownloadsPeer::EBOOK_SLUG, $this->ebook_id);
$ebook = EbookDownloadsPeer::doSelectOne($c);
$ebook->setLastDownloaded(time());
$ebook->setEbookDownloads($ebook->getEbookDownloads()+1);
$ebook->save();
// Adding the file to the Response object
$content_type = (in_array($this->ebook_id, array('readyourbody', 'whyamifatigued', 'nutrientsfromfood'))) ? 'application/pdf': 'image/jpeg';
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setHttpHeader('Pragma: public', true);
$this->getResponse()->setContentType($content_type);
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$this->ebook['name'].'"');
$this->getResponse()->setHttpHeader('Content-Length', filesize($content));
$this->getResponse()->sendHttpHeaders();
$this->getResponse()->setContent(readfile($content));
return sfView::NONE;
}
模板:
<div class="cyan3 txt-large txt-c">
<?php echo link_to('Click here','ebooks/download?ebook='.$ebook_id,'') ?> to <?php echo link_to('download it now','ebooks/download?ebook='.$ebook_id,'') ?>!
</div><br />
</div>
请将 .pdf
作为电子书名称的扩展名。
它会起作用。
检查 $this->ebook
数组的内容并查找包含文件扩展名的字段。将此值添加到行
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$this->ebook['name'] . '"');
所以是因为
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$this->ebook['name'] . $this->ebook['extension'].'"');
我正在尝试下载 PDF 文件,但是当我单击下载按钮时,它下载的文件没有扩展名。那是我下载的时候没有PDF扩展名!
动作:
public function executeDownload(sfWebRequest $request) {
$this->ebook_id = $request->getParameter('ebook', $this->default_ebook);
$this->ebook = $this->ebooks[$this->ebook_id];
if (!is_array($this->ebook))
return;
// Allow user to download the ebook (and make sure there is no other output)
$this->setLayout(false);
sfConfig::set('sf_web_debug', false);
$content = sfConfig::get('sf_root_dir').'/web'.$this->ebook['ebook'];
//echo "<p>PDF: $content</p>"; var_export(filesize($content));exit;
// Check if the file exists
$this->forward404Unless(file_exists($content));
// Record the download for this eBook
$c = new Criteria();
$c->add(EbookDownloadsPeer::EBOOK_SLUG, $this->ebook_id);
$ebook = EbookDownloadsPeer::doSelectOne($c);
$ebook->setLastDownloaded(time());
$ebook->setEbookDownloads($ebook->getEbookDownloads()+1);
$ebook->save();
// Adding the file to the Response object
$content_type = (in_array($this->ebook_id, array('readyourbody', 'whyamifatigued', 'nutrientsfromfood'))) ? 'application/pdf': 'image/jpeg';
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setHttpHeader('Pragma: public', true);
$this->getResponse()->setContentType($content_type);
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$this->ebook['name'].'"');
$this->getResponse()->setHttpHeader('Content-Length', filesize($content));
$this->getResponse()->sendHttpHeaders();
$this->getResponse()->setContent(readfile($content));
return sfView::NONE;
}
模板:
<div class="cyan3 txt-large txt-c">
<?php echo link_to('Click here','ebooks/download?ebook='.$ebook_id,'') ?> to <?php echo link_to('download it now','ebooks/download?ebook='.$ebook_id,'') ?>!
</div><br />
</div>
请将 .pdf
作为电子书名称的扩展名。
它会起作用。
检查 $this->ebook
数组的内容并查找包含文件扩展名的字段。将此值添加到行
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$this->ebook['name'] . '"');
所以是因为
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$this->ebook['name'] . $this->ebook['extension'].'"');