0

当我使用下面的代码下载文件时,它在 IE、Firefox 中运行良好,但在 Chrome 中,您可以看到布局和视图在控制台中被发送回浏览器。状态设置为取消,整行是以红色突出显示。文件下载正常。

退出后;我尝试过各种标题选项,但无济于事

if ($request->isPost()) {

    $this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout()->disableLayout();


$file_fullpath = "C:/eclipse-php/workspace/Dartfire/uploads/data/logo.jpg";

$mtype = '';
// magic_mime module installed?
if (function_exists('mime_content_type')) {
    $mtype = mime_content_type($file_fullpath);
}
// fileinfo module installed?
else if (function_exists('finfo_file')) {
    $finfo = finfo_open(FILEINFO_MIME); // return mime type
    $mtype = finfo_file($finfo, $file_fullpath);
    finfo_close($finfo); 
}

$size = filesize($file_fullpath);

header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename="logo.jpg"');
readfile($file_fullpath);

exit; 


//header("Content-type: application/octet-stream");

        /*
$this->getResponse()
    ->setHeader('Content-Description', 'File Transfer', true)
        ->setHeader('Content-Type', $mtype, true) // change to application/pdf
        ->setHeader('Content-Disposition', "attachment; filename={$document->getFilename()}", true)
        ->setHeader('Content-length', $size, true)
        ->setHeader('Content-Transfer-Encoding', 'binary', true)
        ->setHeader("Content-type", "application/octet-stream")
        ->setHeader('Cache-control', 'private')
    ->appendBody(readfile($file_fullpath));
        //->sendHeaders();

//$this->getResponse()->sendResponse(); 
//Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();

}  
4

1 回答 1

0

header()在将任何输出发送到浏览器之前,您必须进行任何调用。因此,在您的情况下,这需要在readline()通话之前完成。

于 2012-10-30T14:05:16.993 回答