如何在 Catalyst 中根据文件大小取消文件上传
我正在编写一个文件上传处理程序 Catalyst。我正在尝试限制最大文件大小。为此,我制作了一个插件(基于 在这里回答)。这是我检查文件大小的代码:
before 'prepare_body' => sub {
my $c = shift;
my $req = $c->request;
my $length = $req->headers->{"content-length"};
if ($length > 10000)
{
$c->stash->{errors} = "File upload error";
# how do I abort the upload?
}
};
这正确地检测到太大的文件,但我无法弄清楚如何中止上传。理想情况下,它还应该到达控制器/操作。谁能给我指点一下吗?多谢。
I'm writing a file upload handler Catalyst. I'm trying to restrict the maximum file size. To do this I've made a Plugin (based on the answer here). Here is the code where I check for the file size:
before 'prepare_body' => sub {
my $c = shift;
my $req = $c->request;
my $length = $req->headers->{"content-length"};
if ($length > 10000)
{
$c->stash->{errors} = "File upload error";
# how do I abort the upload?
}
};
This correctly detects files that are too big, but I can't for the life of me figure out how to abort the upload. Ideally, it should also reach the controller/action. Can anyone give me a pointer? Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很简单,你可能不应该这样做。您从插件代码中执行的任何中止处理操作都将削弱用户代码以良好方式处理情况的能力(例如,通过提供验证错误或良好的错误页面,而不是 Catalyst 异常页面) 。
然而,一切并没有失去。为什么不尝试这样的事情呢?
如果您的请求过大,这将停止读取,但它会让调度正常进行——这意味着您需要在操作中或在
begin
操作中编写一些代码,或者在链根中,检查$c->stash->{request_body_aborted}
并执行适当的操作 - 无论是设置表单验证错误,还是调用$c-> ;error("请求太大"); $c->detach
或其他。它也是可配置的,就像任何插件一样。Very simply, you probably shouldn't. Anything you do from plugin code to abort the handling is going to knock out the ability of user code to deal with the situation in a nice way (for example by giving a validation error or a nice error page, instead of a Catalyst exception page).
However, all is not lost. Why not try something like this?
This will stop the read if your request is over-size, but it will let dispatch proceed as normal -- which means you will want to write some code in your action, or in a
begin
action, or in a chain root, that checks for$c->stash->{request_body_aborted}
and does something appropriate -- whether that's setting a form validation error, or calling$c->error("Request too large"); $c->detach
or whatever. It's also configurable, as any plugin should be.我认为这需要在链条的早期发生。如果您有标头,则数据包已创建。
也许你可以尝试: $c->detach();或者可能循环遍历 $c->stack 数组并删除可能已添加的与您的上传相关的操作。
I think this needs to occur earlier in the chain. If you have the headers, then the packet is already created.
Perhaps you could try: $c->detach(); or possibly loop through the $c->stack array and remove actions that might have been added, related to your upload.