我怎么能做一个Mojolicious :: Lite静态文件下载器?

时间:2013-01-31 04:22:47

标签: perl mojolicious mojolicious-lite

我正在尝试使用Mojolicious :: Lite制作文件上传/下载,而上传部分没有问题,下载部分会造成麻烦。这段代码让我下载小文本文件,但其他任何东西都变成0字节文件。关于如何做到这一点的任何建议?

get '/download/:file' => sub {
    my $self = shift;
    my $file = $self->param('file');
    $self->res->headers->content_type("application/x-download");
    $self->res->content->asset(Mojo::Asset::File->new(path => "./testdir/$file"));
    $self->rendered;
};

2 个答案:

答案 0 :(得分:7)

您可以安装插件Mojolicious::Plugin::RenderFile以简化此操作。

plugin 'RenderFile';

get '/download/:file' => sub {
  my $self = shift;
  my $file = $self->param('file');
  $self->render_file('filepath' => "./testdir/$file");
};

答案 1 :(得分:4)

Joel Berger posted this little program to start a web server to serve local files,效果很好:

use Mojolicious::Lite;

@ARGV = qw(daemon);

use Cwd;
app->static->paths->[0] = getcwd;

any '/' => sub {
    shift->render_static('index.html');
    };

app->start;