CakePHP MediaView无法正常工作

时间:2014-02-20 03:23:15

标签: php cakephp

我的网站从其他网站下载文件,然后将该文件发送到客户端,但不发送。

//Downloads file from veeva to server
public function get_veeva($id = null)
{
    $this->autoRender = false;

    $asset = $this->Asset->findById($id);
    $vault = $asset['VeevaVault'];

    App::import('Vendor', 'phpVeeva', array('file' => 'veeva' . DS . 'veeva.php'));
    $veeva = new phpVeeva();

    $file = $veeva->getVeevaAsset($vault['veeva_id'], $vault['title']);
    $this->send_veeva($file);
}

//Sends downloaded file to client
public function send_veeva($file)
{
    $filename = $file['filename'];
    $ext = $file['ext'];
    $path = APP . 'tmp/';

    if (file_exists($path . $filename . '.' . $ext)) {
        echo 'Downloading ' . $filename . '.' . $ext . '...';
                //echos /var/www/<sitename>/app/tmp/<filename>.<ext>

        $this->viewClass = 'Media';
        $params = array(
            'id' => sprintf("%s.%s", $filename, $ext),
            'name' => sprintf("%s.%s", $filename, $ext),
            'download' => true,
            'ext' => $ext,
                             'mimeType' => array(
                                   'doc' => 'application/msword',
                                   'pdf' => 'application/pdf',
                                   'ppt' => 'application/vnd.ms-powerpoint'
                                 ),
            'path' => APP . "tmp" . DS
        );
        $this->set($params);
    }
    else {
        echo $filename . '.' . $ext . ' does not exist at ' . $path;
    }
}

该文件存在于其应该和页面回声说它的下载但没有弹出下载窗口。我尝试过使用和不使用mimeType,结果相同。我的Cake版本不支持使用Cake响应来发送文件。

修改 - 添加了HTTP标头

查看标题,会弹出以下内容:

在响应标头中,当我尝试发送.doc文件时,内容长度为0,内容类型为text / html,因此我认为它应该是application / msword。

在请求标头中,接受以下内容:text / html,application / xhtml + xml,application / xml。

以下是标题:

响应:

HTTP / 1.1 200确定

日期:2014年2月20日星期四16:32:26 GMT

服务器:Apache / 2.2.15(Red Hat)

X-Powered-By:PHP / 5.3.3

内容长度:0

连接:关闭

Content-Type:text / html;字符集= UTF-8

请求:

GET / assets / get_veeva / 530566d7-80c4-4ad7-b815-20600a64cb32 HTTP / 1.1

主持人:guru-dev.selfip.com

User-Agent:Mozilla / 5.0(Windows NT 6.1; WOW64; rv:27.0)Gecko / 20100101 Firefox / 27.0

接受:text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8

接受语言:en-US,en; q = 0.5

Accept-Encoding:gzip,deflate

参考者:http://guru-dev.selfip.com/

Cookie:toolbarDisplay = hide;的CakePHP = edh8riumsm2kb61ka4dih3v6c1

连接:保持活力

1 个答案:

答案 0 :(得分:0)

我想这是你的控制者。在控制器中使用echo会破坏MVC模式。在这种情况下,PHP将http标头(内容类型text / html)发送到浏览器,以显示您的“正在下载...”消息。这些标头只能发送一次。浏览器假设处理文本而不处理文件。

删除echo,并确保您的视图正在为您的文件发送相应的标题。

相关问题