/** * Sets the response header based on type map index name. This wraps several methods * available on CakeResponse. It also allows you to use Content-Type aliases. * * @param string|array $type Friendly type name, i.e. 'html' or 'xml', or a full content-type, * like 'application/x-shockwave'. * @param array $options If $type is a friendly type name that is associated with * more than one type of content, $index is used to select which content-type to use. * @return boolean Returns false if the friendly type name given in $type does * not exist in the type map, or if the Content-type header has * already been set by this method. * @see RequestHandlerComponent::setContent() */ public function respondAs($type, $options = array()) { $defaults = array('index' => null, 'charset' => null, 'attachment' => false); $options = $options + $defaults; $cType = $type; if (strpos($type, '/') === false) { $cType = $this->response->getMimeType($type); } if (is_array($cType)) { if (isset($cType[$options['index']])) { $cType = $cType[$options['index']]; } if ($this->prefers($cType)) { $cType = $this->prefers($cType); } else { $cType = $cType[0]; } } if (!$type) { return false; } if (empty($this->request->params['requested'])) { $this->response->type($cType); } if (!empty($options['charset'])) { $this->response->charset($options['charset']); } if (!empty($options['attachment'])) { $this->response->download($options['attachment']); } return true; }
/** * Tests the download method * */ public function testDownload() { $response = new CakeResponse(); $expected = array('Content-Disposition' => 'attachment; filename="myfile.mp3"'); $response->download('myfile.mp3'); $this->assertEquals($expected, $response->header()); }
/** * Display or download the given file * * @param string $view Not used * @param string $layout Not used * @return mixed * @throws NotFoundException */ public function render($view = null, $layout = null) { $name = $download = $extension = $id = $modified = $path = $cache = $mimeType = $compress = null; extract($this->viewVars, EXTR_OVERWRITE); if (is_dir($path)) { $path = $path . $id; } else { $path = APP . $path . $id; } if (!is_file($path)) { if (Configure::read('debug')) { throw new NotFoundException(sprintf('The requested file %s was not found', $path)); } throw new NotFoundException('The requested file was not found'); } if (is_array($mimeType)) { $this->response->type($mimeType); } if (isset($extension) && $this->_isActive()) { $extension = strtolower($extension); $chunkSize = 8192; $buffer = ''; $fileSize = @filesize($path); $handle = fopen($path, 'rb'); if ($handle === false) { return false; } if (!empty($modified) && !is_numeric($modified)) { $modified = strtotime($modified, time()); } else { $modified = time(); } if ($this->response->type($extension) === false) { $download = true; } if ($cache) { $this->response->cache($modified, $cache); } else { $this->response->header(array('Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT', 'Expires' => '0', 'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0', 'Pragma' => 'no-cache')); } if ($download) { $agent = env('HTTP_USER_AGENT'); if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) { $contentType = 'application/octetstream'; } else { if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) { $contentType = 'application/force-download'; } } if (!empty($contentType)) { $this->response->type($contentType); } if (is_null($name)) { $name = $id; } $this->response->download($name); $this->response->header(array('Accept-Ranges' => 'bytes')); $httpRange = env('HTTP_RANGE'); if (isset($httpRange)) { list($toss, $range) = explode('=', $httpRange); $size = $fileSize - 1; $length = $fileSize - $range; $this->response->header(array('Content-Length' => $length, 'Content-Range' => 'bytes ' . $range . $size . '/' . $fileSize)); $this->response->statusCode(206); fseek($handle, $range); } else { $this->response->header('Content-Length', $fileSize); } } else { $this->response->header(array('Content-Length' => $fileSize)); } $this->_clearBuffer(); if ($compress) { $this->_compressionEnabled = $this->response->compress(); } $this->response->send(); return $this->_sendFile($handle); } return false; }