Revision 82fcb2d5
Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt
SL/Controller/Base.pm | ||
---|---|---|
135 | 135 |
} |
136 | 136 |
|
137 | 137 |
sub send_file { |
138 |
my ($self, $file_name, %params) = @_; |
|
138 |
my ($self, $file_name_or_content, %params) = @_; |
|
139 |
|
|
140 |
my ($file, $size); |
|
141 |
|
|
142 |
if (!ref $file_name_or_content) { |
|
143 |
$file = IO::File->new($file_name_or_content, 'r') || croak("Cannot open file '${file_name_or_content}'"); |
|
144 |
$size = -s $file_name_or_content; |
|
145 |
} else { |
|
146 |
$size = length $$file_name_or_content; |
|
147 |
} |
|
139 | 148 |
|
140 |
my $file = IO::File->new($file_name, 'r') || croak("Cannot open file '${file_name}'"); |
|
141 | 149 |
my $content_type = $params{type} || 'application/octet_stream'; |
142 |
my $attachment_name = $params{name} || $file_name;
|
|
150 |
my $attachment_name = $params{name} || (!ref($file_name_or_content) ? $file_name_or_content : '');
|
|
143 | 151 |
$attachment_name =~ s:.*//::g; |
144 | 152 |
|
145 | 153 |
print $::form->create_http_response(content_type => $content_type, |
146 | 154 |
content_disposition => 'attachment; filename="' . $attachment_name . '"', |
147 |
content_length => -s $file);
|
|
155 |
content_length => $size);
|
|
148 | 156 |
|
149 |
$::locale->with_raw_io(\*STDOUT, sub { print while <$file> }); |
|
150 |
$file->close; |
|
157 |
if (!ref $file_name_or_content) { |
|
158 |
$::locale->with_raw_io(\*STDOUT, sub { print while <$file> }); |
|
159 |
$file->close; |
|
160 |
} else { |
|
161 |
$::locale->with_raw_io(\*STDOUT, sub { print $$file_name_or_content }); |
|
162 |
} |
|
151 | 163 |
} |
152 | 164 |
|
153 | 165 |
sub presenter { |
... | ... | |
460 | 472 |
$self->render('todo/single_item', { type => 'js' }, |
461 | 473 |
item => $employee->most_important_todo_item); |
462 | 474 |
|
463 |
=item C<send_file $file_name, [%params]> |
|
475 |
=item C<send_file $file_name_or_content, [%params]> |
|
476 |
|
|
477 |
Sends the file C<$file_name_or_content> to the browser including |
|
478 |
appropriate HTTP headers for a download. If C<$file_name_or_content> |
|
479 |
is a scalar then it is interpreted as a file name which is opened and |
|
480 |
whose content is sent. Otherwise (C<$file_name_or_content> being a |
|
481 |
reference) the referenced scalar's data itself is sent. |
|
464 | 482 |
|
465 |
Sends the file C<$file_name> to the browser including appropriate HTTP |
|
466 |
headers for a download. C<%params> can include the following: |
|
483 |
C<%params> can include the following: |
|
467 | 484 |
|
468 | 485 |
=over 2 |
469 | 486 |
|
... | ... | |
471 | 488 |
'application/octet_stream' |
472 | 489 |
|
473 | 490 |
=item * C<name> -- the name presented to the browser; defaults to |
474 |
C<$file_name> |
|
491 |
C<$file_name>; mandatory if C<$file_name_or_content> is a reference
|
|
475 | 492 |
|
476 | 493 |
=back |
477 | 494 |
|
Auch abrufbar als: Unified diff
SL::Controller::Base::send_file: Unterstützung zum Senden von Skalarinhalten
$file_name kann nun auch eine Skalarreferenz sein. In diesem Falle
wird der referenzierte Inhalt direkt geschickt.
Wenn es ein Skalar ist, wird es wie vorher auch als Dateiname interpretiert.