1CGI::Application::PlugiUns:e:rStCroenatmr(i3b)uted PerlCDGoIc:u:mAepnptlaitciaotnion::Plugin::Stream(3)
2
3
4
6 CGI::Application::Plugin::Stream - CGI::Application Plugin for
7 streaming files
8
10 use CGI::Application::Plugin::Stream (qw/stream_file/);
11
12 sub runmode {
13 # ...
14
15 # Set up any headers you want to set explicitly
16 # using header_props() or header_add() as usual
17
18 #...
19
20 if ( $self->stream_file( $file ) ) {
21 return;
22 } else {
23 return $self->error_mode();
24 }
25 }
26
28 This plugin provides a way to stream a file back to the user.
29
30 This is useful if you are creating a PDF or Spreadsheet document
31 dynamically to deliver to the user.
32
33 The file is read and printed in small chunks to keep memory consumption
34 down.
35
36 This plugin is a consumer, as in your runmode shouldn't try to do any
37 output or anything afterwards. This plugin affects the HTTP response
38 headers, so anything you do afterwards will probably not work. If you
39 pass along a filehandle, we'll make sure to close it for you.
40
41 It's recommended that you increment $| (or set it to 1), which will
42 autoflush the buffer as your application is streaming out the file.
43
45 stream_file()
46 $self->stream_file($fh);
47 $self->stream_file( '/path/to/file',2048);
48
49 This method can take two parameters, the first the path to the file or
50 a filehandle and the second, an optional number of bytes to determine
51 the chunk size of the stream. It defaults to 1024.
52
53 It will either stream a file to the user or return false if it fails,
54 perhaps because it couldn't find the file you referenced.
55
56 We highly recommend you provide a file name if passing along a
57 filehandle, as we won't be able to deduce the file name, and will use
58 'FILE' by default. Example:
59
60 $self->header_add( -attachment => 'my_file.txt' );
61
62 With both a file handle or file name, we will try to determine the
63 correct content type by using File::MMagic. A default of
64 'application/octet-stream' will be used if File::MMagic can't figure it
65 out.
66
67 The size will be calculated and added to the headers as well.
68
69 Again, you can set these explicitly if you want as well:
70
71 $self->header_add(
72 -type => 'text/plain',
73 -Content_Length => 42, # bytes
74 );
75
77 Jason Purdy, <Jason@Purdy.INFO>, with inspiration from Tobias Henoeckl
78 and tremendous support from the cgiapp mailing list.
79
80 Mark Stosberg also contributed to this module.
81
83 CGI::Application, <http://www.cgi-app.org>, "CREATING A STANDARD HTTP
84 HEADER" in CGI.pm,
85 <http://www.mail-archive.com/cgiapp@lists.erlbaum.net/msg02660.html>,
86 File::Basename, "$|" in perlvar
87
89 Copyright (C) 2004-2005 Jason Purdy, <Jason@Purdy.INFO>
90
91 This library is free software. You can modify and or distribute it
92 under the same terms as Perl itself.
93
94
95
96perl v5.28.0 2018-07-14CGI::Application::Plugin::Stream(3)