GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
CGI::Application::Plugin::Stream(3) User Contributed Perl Documentation CGI::Application::Plugin::Stream(3)

CGI::Application::Plugin::Stream - CGI::Application Plugin for streaming files

  use CGI::Application::Plugin::Stream (qw/stream_file/);

  sub runmode {
    # ...

    # Set up any headers you want to set explicitly
    # using header_props() or header_add() as usual

    #...

    if ( $self->stream_file( $file ) ) {
      return;
    } else {
      return $self->error_mode();
    }
  }

This plugin provides a way to stream a file back to the user.

This is useful if you are creating a PDF or Spreadsheet document dynamically to deliver to the user.

The file is read and printed in small chunks to keep memory consumption down.

This plugin is a consumer, as in your runmode shouldn't try to do any output or anything afterwards. This plugin affects the HTTP response headers, so anything you do afterwards will probably not work. If you pass along a filehandle, we'll make sure to close it for you.

It's recommended that you increment $| (or set it to 1), which will autoflush the buffer as your application is streaming out the file.

  $self->stream_file($fh);
  $self->stream_file( '/path/to/file',2048);

This method can take two parameters, the first the path to the file or a filehandle and the second, an optional number of bytes to determine the chunk size of the stream. It defaults to 1024.

It will either stream a file to the user or return false if it fails, perhaps because it couldn't find the file you referenced.

We highly recommend you provide a file name if passing along a filehandle, as we won't be able to deduce the file name, and will use 'FILE' by default. Example:

 $self->header_add( -attachment => 'my_file.txt' );

With both a file handle or file name, we will try to determine the correct content type by using File::MMagic. A default of 'application/octet-stream' will be used if File::MMagic can't figure it out.

The size will be calculated and added to the headers as well.

Again, you can set these explicitly if you want as well:

 $self->header_add(
      -type                     =>      'text/plain',
      -Content_Length   =>      42, # bytes
 );

Jason Purdy, <Jason@Purdy.INFO>, with inspiration from Tobias Henoeckl and tremendous support from the cgiapp mailing list.

Mark Stosberg also contributed to this module.

CGI::Application, <http://www.cgi-app.org>, "CREATING A STANDARD HTTP HEADER" in CGI.pm, <http://www.mail-archive.com/cgiapp@lists.erlbaum.net/msg02660.html>, File::Basename, "$|" in perlvar

Copyright (C) 2004-2005 Jason Purdy, <Jason@Purdy.INFO>

This library is free software. You can modify and or distribute it under the same terms as Perl itself.

2015-03-11 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.