【问题标题】:Downloading files with Zend Framework使用 Zend Framework 下载文件
【发布时间】:2012-02-28 13:43:50
【问题描述】:

我是 Zend Framework 的新手,我遇到了这样的问题。在我的网页上,我有可供用户下载的产品演示。当他们想要下载它们时,他们必须填写表格(姓名、公司、电子邮件、联系电话),然后单击提交开始下载。我希望他们被重定向到产品页面。这是我在控制器中的 showFormAction 代码:

    if ($this->_request->isPost())
    {
        if (!$form->isValid($this->_request->getPost()))  
        {   
            //shows messages and the form again             
        }               
        else 
        { 

            $file = $this->findYoungestFile('/demo/'.$product.'/');
            $this->sendFileToClient($file);
            $this->_redirect('/products/'.$product);
            //sending mail
            $infoMail = new InfoMail($this->_request->getPost(), 'download', $product);
            $this->sendInfoMails($infoMail);                
        }   
    } 
    else
        //show form

这里是 sendFileToClient 函数

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));

        readfile($file); 

重定向不起作用。我想发送到浏览器的标头有问题,但我不知道如何解决。有人可以帮帮我吗?

感谢和问候, 马尔戈萨塔

【问题讨论】:

  • “不起作用”是什么意思?你得到什么输出?
  • 它将文件发送到浏览器,发送电子邮件,但不重定向,停留在同一页面上。
  • 哦。重定向也是一个标题。发送文件下载头后无效:)。
  • 好的,有没有可能解决这个问题?我想知道在空白标签中进行下载操作,但我不知道该怎么做
  • 我知道没有简单的方法,但您可以尝试使用 iframe(可能带有 display:none)并在其中加载文件下载标题。这很 hacky,但可能值得一试:)。

标签: zend-framework header controller action download


【解决方案1】:

当您尝试将用户重定向到另一个页面时,HTTP 标头已经发送。换句话说,您不能同时提供文件并进行重定向。好吧,至少是您尝试这样做的方式。

【讨论】:

  • 好的,请问有什么解决办法吗?我想知道,我是否可以为此下载执行两个操作。一个负责发送文件,另一个负责重定向。
  • 您可以先重定向到所需的页面,然后通过 domain.com/path/to/file"> 或 js 提供文件。
  • 我想通过zend进行文件下载。
  • 当然,Js/meta 标签只会重定向到文件本身。
  • 好的,我会尝试用 iframe 来做。谢谢你的帮助:)
猜你喜欢
  • 2013-04-12
  • 1970-01-01
  • 2011-09-04
  • 2017-11-02
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多