dongxichan8627 2012-03-28 04:07
浏览 32

从Symfony行动发送附件/下载文件

I'm trying to deliver a CSV file through an action. The mime-type in the response still shows as text/html. Can someone help? Thanks

        //$this->setLayout(false);
        //$this->getUser()->shutdown();
        //sfConfig::set('sf_web_debug', false);
        $response = $this->getContext()->getResponse();
        $response = $this->getResponse();
        $response->clearHttpHeaders();
        $response->setHttpheader('Pragma: public', true);
        $response->addCacheControlHttpHeader('Cache-Control', 'must-revalidate');
        $response->setContentType('application/octet-stream', true);
        $response->setHttpHeader('Content-Description', 'File Transfer');
        $response->setHttpHeader('Content-Transfer-Encoding', 'binary', true);
        $response->setHttpHeader('Content-Length', filesize($file_path));
        $response->setHttpHeader('Content-Disposition', 'attachment; filename="' . $file_name . '"');
        $this->getResponse()->sendHttpHeaders();
        //$response->setContent(file_get_contents($file_path));
        //readfile($file_path);
        $this->renderText(file_get_contents($file_path));
        //return sfView::NONE;

        return sfView::HEADER_ONLY;

Trust me, before getting here, I've all search results purple. As you can see all the permutations & combinations above, I still couldn't get this working!

  • 写回答

1条回答 默认 最新

  • doumeng4400 2012-03-28 18:27
    关注

    Try something like this:

    $path = 'absolute/path/to/the/file';
    
    /** @var $response sfWebResponse */
    $response = $this->getResponse();
    
    $response->setContentType('text/csv');
    $response->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($path) . '"');
    $response->setContent(file_get_contents($path));
    
    return sfView::NONE;
    

    Note: filename must be ASCII see RFC 6266

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改