Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 4d6e7659

Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt

  • ID 4d6e7659706445947795fa5319a0d988e8cbff49
  • Vorgänger 606e7e25
  • Nachfolger 1db32144

Funktion "send_file" im Controller, um Dateien herunterzuladen

Unterschiede anzeigen:

SL/Controller/Base.pm
5 5
use parent qw(Rose::Object);
6 6

  
7 7
use Carp;
8
use IO::File;
8 9
use List::Util qw(first);
9 10

  
10 11
#
......
83 84
  return $output;
84 85
}
85 86

  
87
sub send_file {
88
  my ($self, $file_name, %params) = @_;
89

  
90
  my $file            = IO::File->new($file_name, 'r') || croak("Cannot open file '${file_name}'");
91
  my $content_type    =  $params{type} || 'application/octet_stream';
92
  my $attachment_name =  $params{name} || $file_name;
93
  $attachment_name    =~ s:.*//::g;
94

  
95
  print $::form->create_http_response(content_type        => $content_type,
96
                                      content_disposition => 'attachment; filename="' . $attachment_name . '"',
97
                                      content_length      => -s $file);
98

  
99
  $::locale->with_raw_io(\*STDOUT, sub { print while <$file> });
100
  $file->close;
101
}
102

  
86 103
#
87 104
# Before/after run hooks
88 105
#
......
368 385
  $self->render('todo/single_item', { type => 'js' },
369 386
                item => $employee->most_important_todo_item);
370 387

  
388
=item C<send_file $file_name, [%params]>
389

  
390
Sends the file C<$file_name> to the browser including appropriate HTTP
391
headers for a download. C<%params> can include the following:
392

  
393
=over 2
394

  
395
=item * C<type> -- the file's content type; defaults to
396
'application/octet_stream'
397

  
398
=item * C<name> -- the name presented to the browser; defaults to
399
C<$file_name>
400

  
401
=back
402

  
371 403
=item C<url_for $url>
372 404

  
373 405
=item C<url_for $params>
SL/Form.pm
629 629
  $cgi_params{'-charset'} = $params{charset} if ($params{charset});
630 630
  $cgi_params{'-cookie'}  = $session_cookie  if ($session_cookie);
631 631

  
632
  map { $cgi_params{'-' . $_} = $params{$_} if exists $params{$_} } qw(content_disposition content_length);
633

  
632 634
  my $output = $cgi->header(%cgi_params);
633 635

  
634 636
  $main::lxdebug->leave_sub();

Auch abrufbar als: Unified diff