[Midas] redirect()

Zach Mullen zach.mullen at kitware.com
Tue Jul 10 14:13:25 EDT 2012


Redirect is just sending an HTTP redirect to a different controller.  The
best example of how to download a file to the client can be seen in
core/controllers/components/DownloadBitstreamComponent::download.

Your version will probably be simpler; you will just set the appropriate
headers for Content-Type (set to, for example "image/jpeg" or "image/png",
whatever your image MIME type is).  Set the other headers as you wish (such
as Content-Length, Cache-Control, Expires, Content-Disposition.  When you
are ready to send the actual bytes, you'll need to flush any output
buffering that happens using the following block:

while(ob_get_level() > 0)
      {
      ob_end_clean();
      }

Then open your file and send it to the user, like so:

$chunkSize = 1024*1024; // 1 MiB chunk written to buffer at a time
$fh = fopen('path_to_file.png', 'rb'); //use the actual path to the file :)
while(!feof($fh) && connection_status() == 0)
  {
  echo fread($fh, $chunkSize); //read a chunk and send it to the client
  }
fclose($fh);

exit();

The exit() call obviously should only be done after all your required logic
is finished, but it is necessary within Zend framework to avoid
header-after-output errors.

My understanding is that you are just downloading a temporary file and then
deleting it, is this correct?

-Zach

On Tue, Jul 10, 2012 at 1:48 PM, Mona Wong <mona at sdsc.edu> wrote:

>
> Redirect would be appropriate if you wish to send them an HTTP redirect
> response header.
>
>
> So $this->redirect() which calls $this->_redirect() just does a HTTP
> redirect and nothing else?
>
> What if I want my controller to download an image file like
> midas.bitstream.download so that I can call my controller as you had
> suggested in a previous email:
>
> If you want to render the image in the browser, use
> midas.bitstream.download.  It will send the correct MIME Type header and
> then stream the raw bytes to the client.  Example:
>
> <img id="theImage" src="" />
>
> $('#theImage').attr('src', '
> http://midas/api/rest?method=midas.bitstream.download...');
>
>
> Is there a trick to getting this working?
>
> Mona
>
>  *********************************************
>     Mona Wong
>     Web & iPad Application Developer
>     San Diego Supercomputer Center
>
>     "Forgive everyone everything."
>  -- Regina Brett
> *********************************************
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/midas/attachments/20120710/255d3195/attachment.html>


More information about the Midas mailing list