我正在尝试使用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;
};
答案 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;