我正在编写一个上传函数,并且httpRuntime在web.config中使用大于指定最大大小的文件时捕获"System.Web.HttpException:超出最大请求长度"时遇到问题(最大大小设置为5120).我正在使用一个简单<input>的文件.
问题是在上传按钮的click-event之前抛出了异常,并且异常发生在我的代码运行之前.那么如何捕获和处理异常呢?
编辑:异常立即抛出,所以我很确定它不是由于连接速度慢导致的超时问题.
我有一个简单的ASP.NET MVC 3网站在IIS 7.0中托管,并且很难显示404.13 http状态代码的自定义http错误页面.
我的Web.Config中有以下配置
<system.web>
<httpRuntime maxRequestLength="2048"/>
<customErrors mode="Off"/>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear/>
<error statusCode="404" subStatusCode="-1" path="/home/showerror" responseMode="ExecuteURL" />
<error statusCode="404" subStatusCode="13" path="/home/showerror" responseMode="ExecuteURL" />
</httpErrors>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1048576"/>
</requestFiltering>
</security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
当我导航到不存在的页面时,我的错误页面会正确呈现.但是,如果我上传的文件大于1MB,我会收到一个空的404响应.网址永远不会被执行.如果我将responseMode更改为Redirect,则会正确重定向用户.
我基本上有一个ASP.NET FileUpload控件,我需要为其提供以下消息抛出的异常:
超出最大请求长度.
限制是我需要限制用户上传一个文件,因为我已将其他详细信息从一些文本框保存到DB中.
最大文件大小设置在web.config中设置如下:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="41943040" />
</requestFiltering>
</security>
</system.webServer>
<system.web>
<httpRuntime maxRequestLength="40960" requestValidationMode="2.0" />
</system.web>
Run Code Online (Sandbox Code Playgroud)
因此我搜索了许多解决方案,如下所示:
使用Global.asax验证"Application_BeginRequest()"中的文件大小,但它没有解决我的问题,当文件大小较大且重定向到错误页面不起作用时,它在重定向时崩溃.
使用AjaxFileUpload而不是ASP.NET FileUpload控件,这在文件大小检查时再次崩溃,大于Web.config中允许的最大大小.其次我必须限制总共用户只能上传一个文件,而不是一个文件,所以使用AjaxFileUpload它不能正常工作,因为我必须上传一个文件并将其他细节保存在一些文件中与该文件相关的文本框.第三,当文件大小超过限制(即40MB)时,AjaxFileUpload只会变为红色,不会显示任何消息.
我想知道我怎样才能达到我的要求,因为我几天后就被困住了.
更新:发现其中很少有用,但无法完成基于它们的要求:
以下是标记:
<asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
<asp:FileUpload ID="theFile" runat="server" />
<asp:Button ID="Button2" runat="server" Text="Upload 1" onclick="Button2_Click" />
<asp:Button ID="Button1" runat="server" Text="Upload 1" onclick="btnUpload1_Click" />
<asp:Button ID="btnUpload" runat="server" Text="btnUpload_Click" onclick="btnUpload_Click" />
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:AjaxFileUpload ID="AjaxFileUpload2" runat="server" ToolTip="Upload File" ThrobberID="MyThrobber" onclientuploaderror="IsFileSizeGreaterThanMax" onuploadcomplete="AjaxFileUpload1_UploadComplete" AllowedFileTypes="jpg,jpeg,gif,png,pjpeg,zip,rar,pdf,xls,xlsx,doc,docx" MaximumNumberOfFiles="1" Height="50px" Width="350px"/>
<asp:Image id="MyThrobber" …Run Code Online (Sandbox Code Playgroud)