【发布时间】:2016-02-25 10:38:05
【问题描述】:
我正在尝试使用 mojolicious 上传文件,然后经过一些更改,我想下载相同的文件。文件上传部分没问题。但我不确定如何处理下载文件的路径。
Controller:
sub upload{
my $self=shift;
my $upload=$self->param('fileName');
my $file_name=$upload->filename;
$self->render(file=>"$file_name",
filepath=>$filepath
);
}
我在模板upload.html.ep中有这个。
<body>
Successfully uploaded file: <%=$file%><br/>
<a href="download/<%= $filepath %>" class="button">
Download!
</a>
现在a href 在此处生成为a href=download/file/path/foo.txt。如何处理这条路径的路由?
my $r = $self->routes;
# Normal route to controller
$r->get('/')->to('example#welcome');
$r->post('/upload')->to('example#upload'); #working well
$r->get('/download/:file')->to('example#download'); # not working
我收到错误:
None of these routes could generate a response for your GET request for
/download/file/path/foo.txt.
我认为占位符是这样工作的。
【问题讨论】:
-
如果
$filepath包含斜线/路由会混淆,因为它需要一个参数。尝试使用没有完整路径的 filename (无论如何你都不应该公开它)。它应该类似于http://example.org/download/myfile.txt,其中myfile.txt是文件名。与路由/product/:id/details相比,$id也不允许有斜线,因为这会使路由匹配中断。 (这是一个有根据的猜测,因为我对 Mojo 不够熟悉,但如果它有效,我会写下来作为答案。)
标签: html perl mojolicious