gpt4 book ai didi

c# - 如何限制文件上传的大小但显示有意义的响应?

转载 作者:行者123 更新时间:2023-11-30 22:24:17 26 4
gpt4 key购买 nike

我将其添加到我的 web.config 文件中:

<httpRuntime maxRequestLength="40960"/> <!-- Limit to 40 megabytes -->

当我尝试上传大于 40 兆字节的内容时,我在 Firefox 中得到了这个:

The connection was reset

The connection to the server was reset while the page was loading.

* The site could be temporarily unavailable or too busy. Try again in a few
moments.
* If you are unable to load any pages, check your computer's network
connection.
* If your computer or network is protected by a firewall or proxy, make sure
that Firefox is permitted to access the Web.

是否有一些方法既可以从这个内置的文件大小限制中获益,又可以实际显示一个不错的错误页面?当然,我可以在 Controller 中执行此检查,但此时流量已经耗尽,因为巨大的文件已到达我的服务器。

if (image.ContentLength > 40960) // 'image' is HttpPostedFileBase

有什么建议吗?

最佳答案

在你的 global.asax.cs 中这个怎么样?

void Application_Error(object sender, EventArgs e)
{
if (Request.TotalBytes > 40960 * 1024)
{
Server.ClearError();
Response.Clear();
Response.Write("File uploaded is greater than 40 MB");
}

}

关于c# - 如何限制文件上传的大小但显示有意义的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12831355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com