【问题标题】:File is downloading without extension?文件正在下载而没有扩展名?
【发布时间】:2017-12-07 13:39:01
【问题描述】:

我正在尝试下载 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>

【问题讨论】:

  • $this-&gt;ebook['name'] 是否包含带扩展名的文件名?也许扩展位于电子书数组中的不同字段
  • 不,只是名字@MarkC。
  • 将 .pdf 文件赋予它应该可以工作的电子书名称
  • 检查$this-&gt;ebook 数组的内容并查找包含文件扩展名的字段。将此值添加到行 $this-&gt;getResponse()-&gt;setHttpHeader('Content-Disposition', 'attachment; filename="'.$this-&gt;ebook['name'].'"');
  • 谢谢你可以添加答案我会接受的! @rahulsm 马克 C.

标签: php symfony symfony1 download


【解决方案1】:

检查$this-&gt;ebook 数组的内容并查找包含文件扩展名的字段。将此值添加到行

$this-&gt;getResponse()-&gt;setHttpHeader('Content-Disposition', 'attachment; filename="'.$this-&gt;ebook['name'] . '"');

所以它是因为像

$this-&gt;getResponse()-&gt;setHttpHeader('Content-Disposition', 'attachment; filename="'.$this-&gt;ebook['name'] . $this-&gt;ebook['extension'].'"');

【讨论】:

  • 感谢您的指导!
【解决方案2】:

请将.pdf 作为电子书名称的扩展名。
它会起作用的。

【讨论】:

  • 感谢您的指导!
  • 这是一个临时解决方案。如果源文件是图像或 Excel 表怎么办??
猜你喜欢
  • 2013-05-07
  • 1970-01-01
  • 2021-01-25
  • 2011-12-24
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
相关资源
最近更新 更多