【问题标题】:Handling anchor tag in mojolicious在 mojolicious 中处理锚标记
【发布时间】: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


【解决方案1】:

你需要使用wildcard placeholders:

$r->get('/download/*file')->to('example#download');

【讨论】:

  • 以及如何进入sub意味着进入sub download{$self-&gt;param('here what should i put')}
  • @AruneshSingh $self-&gt;param('file').
  • 整个路径都在/download/file/path/foo.txt,但我只想要/file/path/foo.txt。我应该使用regex 或任何其他想法删除它
  • @AruneshSingh 不,$self-&gt;param('file') 应该为 /download/file/path/foo.txt 请求返回 file/path/foo.txt
猜你喜欢
  • 2012-03-14
  • 2010-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 2014-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多