actix-web:限制上传文件大小
我成功上传了引用此示例的文件,但我不这样做不知道如何限制文件的大小,例如我无法保存超过5M
的文件
[dependencies]
actix-web = "4"
actix-multipart = "0.4"
我尝试过这个,但它不起作用。
web::resource("/upload_file")
.app_data(web::PayloadConfig::new(1024 * 5))
.route(web::post().to(views::save_file)),
I successfully uploaded the file referring to this example, but I don't know how to limit the size of the file, for example I can't save the file more than 5M
[dependencies]
actix-web = "4"
actix-multipart = "0.4"
I tried this, but it didn't work.
web::resource("/upload_file")
.app_data(web::PayloadConfig::new(1024 * 5))
.route(web::post().to(views::save_file)),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
中间件
默认情况下在所有请求中都使用,可以在指定路由中使用
req.path()
使用
middleware
It is used in all requests by default, and can be used in a specified route using
req.path()
use
2024年更新:以前的方法不再是必要的。相反,将其添加到您的app():
错误处理程序:
imports:
documentation:
https://docs.rs/actix-multipart/latest/actix_multipart/form/sustruct.multipartformconfig.html
Update for 2024: The previous method is no longer neccessary. Instead, add this to your App():
Error handler:
imports:
Documentation:
https://docs.rs/actix-multipart/latest/actix_multipart/form/struct.MultipartFormConfig.html
此发布的答案是指ACTIX中的多部分上传示例。目前,由于:
https://github.com/actix/actix/actix/actix/actix-web/issues /2695
在返回错误之前,有必要排除有效载荷,否则连接可能会悬挂,螺纹可能会惊慌。在这里,在返回有效载体错误之前将首先耗尽有效载荷的示例。一旦解决上述问题,这可能不是必需的:
This posted answer refers to the multipart upload example within Actix. Currently, due to:
https://github.com/actix/actix-web/issues/2695
It's necessary to drain the payload before returning an error, or the connection may hang or the thread may panic. Here an example that will drain the payload first, prior to returning a PayloadTooLarge error. That may not be necessary once the aforementioned is resolved: