framework/web/Response.php
public function sendStreamAsFile($handle, $attachmentName, $options = [])
    {
        $headers = $this->getHeaders();
        if (isset($options['fileSize'])) {
            $fileSize = $options['fileSize'];
        } else {
            fseek($handle, 0, SEEK_END);
            $fileSize = ftell($handle);
        }

        $range = $this->getHttpRange($fileSize);
        if ($range === false) {
            $headers->set('Content-Range', "bytes */$fileSize");
            throw new RangeNotSatisfiableHttpException();
        }

        list($begin, $end) = $range;
        if ($begin != 0 || $end != $fileSize - 1) {
            $this->setStatusCode(206);
            $headers->set('Content-Range', "bytes $begin-$end/$fileSize");
        } else {
            $this->setStatusCode(200);
        }

        $mimeType = isset($options['mimeType']) ? $options['mimeType'] : 'application/octet-stream';
        $this->setDownloadHeaders($attachmentName, $mimeType, !empty($options['inline']), $end - $begin + 1);

        $this->format = self::FORMAT_RAW;
        $this->stream = [$handle, $begin, $end];

        return $this;
    }
public function sendStreamAsFile($handle, $attachmentName, $options = [])
    {
        $headers = $this->getHeaders();
        if (isset($options['fileSize'])) {
            $fileSize = $options['fileSize'];
        } else {
            fseek($handle, 0, SEEK_END);
            $fileSize = ftell($handle);
        }

        $range = $this->getHttpRange($fileSize);
        if ($range === false) {
            $headers->set('Content-Range', "bytes */$fileSize");
            throw new RangeNotSatisfiableHttpException();
        }

        list($begin, $end) = $range;
        if ($begin != 0 || $end != $fileSize - 1) {
            $this->setStatusCode(206);
            $headers->set('Content-Range', "bytes $begin-$end/$fileSize");
        } else {
            $this->setStatusCode(200);
        }

        $mimeType = isset($options['mimeType']) ? $options['mimeType'] : 'application/octet-stream';
        $this->setDownloadHeaders($attachmentName, $mimeType, !empty($options['inline']), $end - $begin + 1);

        $this->format = self::FORMAT_RAW;
        $this->stream = [$handle, $begin, $end];

        return $this;
    }
public function sendStreamAsFile ( $ handle , $ attachmentName , $ options = [ ] ) { $ headers = $ this -> getHeaders ( ) ; if ( isset ( $ options [ 'fileSize' ] ) ) { $ fileSize = $ options [ 'fileSize' ] ; } else { fseek ( $ handle , 0 , SEEK_END ) ; $ fileSize = ftell ( $ handle ) ; } $ range = $ this -> getHttpRange ( $ fileSize ) ; if ( $ range === false ) { $ headers -> set ( 'Content-Range' , "bytes */$fileSize" ) ; throw new RangeNotSatisfiableHttpException ( ) ; } list ( $ begin , $ end ) = $ range ; if ( $ begin != 0 || $ end != $ fileSize - 1 ) { $ this -> setStatusCode ( 206 ) ; $ headers -> set ( 'Content-Range' , "bytes $begin-$end/$fileSize" ) ; } else { $ this -> setStatusCode ( 200 ) ; } $ mimeType = isset ( $ options [ 'mimeType' ] ) ? $ options [ 'mimeType' ] : 'application/octet-stream' ; $ this -> setDownloadHeaders ( $ attachmentName , $ mimeType , ! empty ( $ options [ 'inline' ] ) , $ end - $ begin + 1 ) ; $ this -> format = self :: FORMAT_RAW ; $ this -> stream = [ $ handle , $ begin , $ end ] ; return $ this ; }
Sends the specified stream as a file to the browser.

Note that this method only prepares the response for file sending. The file is not sent
until [[send()]] is called explicitly or implicitly. The latter is done after you return from a controller action.

@param resource $handle the handle of the stream to be sent.
@param string $attachmentName the file name shown to the user.
@param array $options additional options for sending the file. The following options are supported:

- `mimeType`: the MIME type of the content. Defaults to 'application/octet-stream'.
- `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false,
meaning a download dialog will pop up.
- `fileSize`: the size of the content to stream this is useful when size of the content is known
and the content is not seekable. Defaults to content size using `ftell()`.
This option is available since version 2.0.4.

@return $this the response object itself
@throws RangeNotSatisfiableHttpException if the requested range is not satisfiable
@see sendFile() for an example implementation.
Sends the specified stream as a file to the browser .