文件正在下载没有扩展名?

时间:2017-12-07 13:39:01

标签: php symfony symfony1 downloading

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

2 个答案:

答案 0 :(得分:1)

检查$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'].'"');

答案 1 :(得分:0)

请附加apiVersion: v1 kind: Pod metadata: name: spring-boot-web-demo labels: purpose: demonstrate-spring-boot-web spec: containers: - name: spring-boot-web image: docker.io/joethecoder2/spring-boot-web env: ["name": "-Dcassandra_ip", "value": "127.0.0.1"] command: ["java","-jar", "spring-boot-web-0.0.1-SNAPSHOT.jar", "-D","cassandra_ip=127.0.0.1", "-D","cassandra_port=9042"] args: ["-Dcassandra_ip=127.0.0.1", "-Dcassandra_port=9042"] restartPolicy: OnFailure 作为电子书名称的附加信息 它会起作用。