2009年5月20日星期三

Re: [fw-mvc] Force download question

Hi Ed,

Just dealt with a similar issue. Try this.

$bits =
@file_get_contents(Zend_Registry::get('rootdir').'/application/data/'.$data['uuid']);

$response = $this->_response;

if(strlen($bits) == 0) {
$response->setBody('Sorry, we could not find requested download file.');
}
else {
$response->setHeader('Content-type',$data['type'], true);
$response->setHeader('Content-Type','application/octet-stream', true);
$response->setHeader('Content-Disposition','attachment;
filename='.$data['filename'], true);
$response->setHeader('Content-length',$data['size'], true);

$response->setBody($bits);
}

You might also need to to use the following:
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

I put these in a preDispatcher function but you could include them
inline as far as I know.

Lionel.

Quoting Ed Lazor <edlazor@internetarchitects.biz>:

> I'm trying to force file downloads and I'm running into a problem with
> IE6 (this works in other browsers). Code for 3 tries is listed at the
> bottom of this email. The first try works, but it's outside of ZF.
> The other two tries are actions within a test controller. Try #2
> doesn't really seem much different than try #1, but it doesn't work.
>
> IE6 lists the wrong file name, the wrong file size, and the download
> errors out - it says it wasn't able to open the website.
>
> Any ideas why?
>
> -Ed
>
>
>
> --------------------------- Try #1
> ------------------------------------------------------------------------
> <?php
>
> header('Content-type: image/gif');
> header('Content-Disposition: attachment; filename="Google_Logo.gif"');
> readfile('http://www.google.com/logos/logo.gif');
>
> ?>
>
>
> --------------------------- Try #2
> ------------------------------------------------------------------------
>
> public function downloadGifAction() {
> $this->getHelper('layout')->disableLayout();
> $this->getHelper('ViewRenderer')->setNoRender();
> header('Content-type: image/gif');
> header('Content-Disposition: attachment; filename="Google_Logo.gif"');
> readfile('http://www.google.com/logos/logo.gif');
> }
>
>
>
> --------------------------- Try #3
> ------------------------------------------------------------------------
>
> public function downloadGifbAction() {
> $this->getHelper('layout')->disableLayout();
> $this->getHelper('ViewRenderer')->setNoRender();
>
> $data = file_get_contents('http://www.google.com/logos/logo.gif');
>
> $response = $this->getResponse()
> $->setHeader('Content-Type', 'image/gif', true)
> $->setHeader('Content-Disposition', 'attachment;
> Filename="Google_Logo.gif"', true)
> $->setHeader('Content-Transfer-Encoding', 'binary', true)
> $->setHeader('Expires', 0)
> $->setHeader('Cache-Control', 'must-revalidate, post-check=0,
> pre-check=0')
> $->setHeader('Pragma', 'public')
> $->setHeader('Content-Length', strlen($data))
> $->setBody($data)
> $->sendResponse();
> }
>
>

没有评论: