[Catalyst] Catalyst::Response - send a file
Hi, I am looking for a method to send a file in response to a request. My effort is below and all this does is print the file's path on the page. I can't set the content-disposition or see find an obvious method in C::Response or C::Request. Am I looking in the wrong place? Can someone point me in the right direction. Tia, Dp. sub downloadFile { my ($name, $filepath) = @_; my $length = (stat($filepath))[7]; my $res = $c-response; $res-content_length($length); $res-headers-({ 'Content-Disposition' = attachment;filename=$name} ); # CODE ref error $res-sendfile($filepath,0,$length); # Ok no such method but hopefully you'll get what I mean. } ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Catalyst::Response - send a file
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi Dermot, Dermot wrote: | I am looking for a method to send a file in response to a | request. Have you considered Catalyst::Plugin::Static::Simple ? ~ http://search.cpan.org/perldoc?Catalyst::Plugin::Static::Simple Or, use a suitably configured Apache for better performance in production. regards, oliver. - -- Oliver Gorwits, Network and Telecommunications Group, Oxford University Computing Services -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.7 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFIqrEk2NPq7pwWBt4RAkJ6AJ9IYC5ddBBemb2AECT0b2J6CtbnSwCeNOhM 9IXDMEt7GyvHvvIxea0JyM0= =yK/e -END PGP SIGNATURE- ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Catalyst::Response - send a file
2008/8/19 Carl Franks [EMAIL PROTECTED]: 2008/8/19 Dermot [EMAIL PROTECTED]: sub downloadFile { my ($name, $filepath) = @_; my $length = (stat($filepath))[7]; my $res = $c-response; $res-content_length($length); $res-headers-({ 'Content-Disposition' = attachment;filename=$name} ); # CODE ref error $res-sendfile($filepath,0,$length); # Ok no such method but hopefully you'll get what I mean. } open my $filehandle, '', $filepath or die $!; $c-response-body( $filehandle ); Sorry. I mustn't be making myself clear. I want the browser to offer the user a 'save as' dialogue box. The modperl equivalent would be the RequestIO::sendfile The above sends the contents of filepath to the browser Thanx, Dp. ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Catalyst::Response - send a file
2008/8/19 Dermot [EMAIL PROTECTED]: 2008/8/19 Carl Franks [EMAIL PROTECTED]: 2008/8/19 Dermot [EMAIL PROTECTED]: sub downloadFile { my ($name, $filepath) = @_; my $length = (stat($filepath))[7]; my $res = $c-response; $res-content_length($length); $res-headers-({ 'Content-Disposition' = attachment;filename=$name} ); # CODE ref error $res-sendfile($filepath,0,$length); # Ok no such method but hopefully you'll get what I mean. } open my $filehandle, '', $filepath or die $!; $c-response-body( $filehandle ); Sorry. I mustn't be making myself clear. I want the browser to offer the user a 'save as' dialogue box. The modperl equivalent would be the RequestIO::sendfile The above sends the contents of filepath to the browser Ah, ok. You should have used header(). headers() returns the HTTP::Headers object, which you were overwriting. $c-response-header( 'Content-Disposition' = attachment;filename=$name ); You'll still need the body($fh) bit to send the data, too. Carl ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Catalyst::Response - send a file
On Tuesday 19 August 2008 16:41:40 Carl Franks wrote: You'll still need the body($fh) bit to send the data, too. Or, if you use Catalyst::Plugin::Static::Simple, you can use: $c-serve_static_file($file_path); some code snippets from that method: $c-res-headers-content_type( $type ); $c-res-headers-content_length( $stat-size ); $c-res-headers-last_modified( $stat-mtime ); .. my $fh = IO::File-new( $full_path, 'r' ); .. binmode $fh; $c-res-body( $fh ); -- Bogdan Lucaciu http://www.wiz.ro ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Catalyst::Response - send a file
On 19 Aug 2008, at 14:27, Dermot wrote: 2008/8/19 Carl Franks [EMAIL PROTECTED]: 2008/8/19 Dermot [EMAIL PROTECTED]: sub downloadFile { my ($name, $filepath) = @_; my $length = (stat($filepath))[7]; my $res = $c-response; $res-content_length($length); $res-headers-({ 'Content-Disposition' = attachment;filename=$name} ); # CODE ref error Where did you get this syntax idea from? Since when has headers been returning a coderef? Go read the docs again: http://search.cpan.org/~mramberg/Catalyst-Runtime-5.7014/lib/Catalyst/Response.pm#$res-%3Eheader Fix the above headers you have, and use the snippet luke gave below open my $filehandle, '', $filepath or die $!; $c-response-body( $filehandle ); ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Catalyst::Response - send a file
2008/8/19 Bogdan Lucaciu [EMAIL PROTECTED]: On Tuesday 19 August 2008 16:41:40 Carl Franks wrote: You'll still need the body($fh) bit to send the data, too. Or, if you use Catalyst::Plugin::Static::Simple, you can use: $c-serve_static_file($file_path); some code snippets from that method: $c-res-headers-content_type( $type ); $c-res-headers-content_length( $stat-size ); $c-res-headers-last_modified( $stat-mtime ); .. my $fh = IO::File-new( $full_path, 'r' ); .. binmode $fh; $c-res-body( $fh ); Thanx Carl, Bogdan and Oliver for your responses. Sorry if I wasn't clear to begin with. This method will work well for binary files. I have opted $c-response-header() method for now. Much appreciated. Dp. ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Catalyst::Response - send a file
* On Tue, Aug 19 2008, Dermot wrote: This method will work well for binary files. I have opted $c-response-header() method for now. You haven't described what that method is. There are two things you need to do to send a file for download. 1) Set up the HTTP headers (Content-disposition) so that the browser knows what to do. 2) Actually send the data. In your original post, you hand-waved sending the data. A number of posters told you how to do it. Now you're saying you've opted [for the] header method. That doesn't make any sense. So what exactly are you doing? Regards, Jonathan Rockway -- print just = another = perl = hacker = if $,=$ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Catalyst::Response - send a file
Jonathan Rockway [EMAIL PROTECTED] wrote on 08/19/2008 01:20:53 PM: * On Tue, Aug 19 2008, Dermot wrote: This method will work well for binary files. I have opted $c-response-header() method for now. You haven't described what that method is. There are two things you need to do to send a file for download. 1) Set up the HTTP headers (Content-disposition) so that the browser knows what to do. For the save to disk force prompt -- code I that has served me well in the past (for all tested browsers, ie mac, ie 5-7, moz, ff 1-2, safari, opera, lynx, many proxy servers ...) $c-res-headers-content_type('application/octet-stream'); $c-res-headers-content_length( $stat-size ); $c-res-headers-last_modified( $stat-mtime ); $c-response-headers-header('Content-disposition:' = attachment; filename=$filename ); $c-res-headers-expires( time() ); $c-res-headers-header( 'Last-Modified' = HTTP::Date::time2str); $c-res-headers-header( 'Pragma'= 'no-cache' ); $c-res-headers-header( 'Cache-Control' = 'no-cache' ); This forces the save promt and also makes sure no-cache is set so that a failed download does not leave the user with a partial file. -Wade 2) Actually send the data. In your original post, you hand-waved sending the data. A number of posters told you how to do it. Now you're saying you've opted [for the] header method. That doesn't make any sense. So what exactly are you doing? Regards, Jonathan Rockway -- print just = another = perl = hacker = if $,=$ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Catalyst::Response - send a file
2008/8/19 Jonathan Rockway [EMAIL PROTECTED]: * On Tue, Aug 19 2008, Dermot wrote: $c-res-headers-content_type( $type ); $c-res-headers-content_length( $stat-size ); $c-res-headers-last_modified( $stat-mtime ); .. my $fh = IO::File-new( $full_path, 'r' ); .. binmode $fh; $c-res-body( $fh ); This method will work well for binary files. I have opted $c-response-header() method for now. You haven't described what that method is. Please don't quote me out of context. The This referred to Bogdan Lucaciu reply which was directly above my comments which you have snipped. There are two things you need to do to send a file for download. 1) Set up the HTTP headers (Content-disposition) so that the browser knows what to do. 2) Actually send the data. In your original post, you hand-waved sending the data. A number of posters told you how to do it. Now you're saying you've opted [for the] header method. That doesn't make any sense. So what exactly are you doing? As per Carl Franks 2nd reply $c-response-header( 'Content-Disposition' = attachment;filename=$name ); You'll still need the body($fh) bit to send the data, too. $c-response has headers and header. http://search.cpan.org/~mramberg/Catalyst-Runtime-5.7014/lib/Catalyst/Response.pm#$res-%3Eheader I opted for the header method as stated by Carl Franks. Thanx, Dp. ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/