Skip to content

Instantly share code, notes, and snippets.

@koorchik
Created May 15, 2012 05:38
Show Gist options
  • Select an option

  • Save koorchik/2699369 to your computer and use it in GitHub Desktop.

Select an option

Save koorchik/2699369 to your computer and use it in GitHub Desktop.
render_file
$app->helper('render_file' => sub {
my $c = shift;
my %args = @_;
my $filepath = $args{filepath};
unless ( -f $filepath && -r $filepath ) {
$c->app->log->error("Cannot read file [$filepath]. error [$!]");
return;
}
my $filename = $args{filename} || fileparse($filepath);
my $status = $args{status} || 200;
my $headers = Mojo::Headers->new();
$headers->add( 'Content-Type', 'application/x-download;name=' . $filename );
$headers->add( 'Content-Disposition', 'attachment;filename=' . $filename );
$c->res->content->headers($headers);
# Stream content directly from file
$c->res->content->asset( Mojo::Asset::File->new( path => $filepath ) );
return $c->rendered($status);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment