maxrequestlength
引起的
.默认情况下上传时,图片不能超过4MB.
我知道这可以从web.config文件更改.
当我尝试通过显示另一个页面来尝试解决此错误时,会弹出一个不同的错误.调试时,程序立即进入application_error.
执行Server.GetLastError()时生成异常:
[System.Web.HttpUnhandledException] {“Exception of type
‘System.Web.HttpUnhandledException’ was
thrown.”} System.Web.HttpUnhandledExceptionthe stack trace: at System.Web.UI.Page.HandleError(Exception e) at
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean
includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest() at
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at
System.Web.UI.Page.ProcessRequest(HttpContext context) at
ASP.businessprofile_aspx.ProcessRequest(HttpContext context) in
c:\Users\Mattew\AppData\Local\Temp\Temporary ASP.NET
Files\root\4ea30077\8f66786f\App_Web_h5fmhavk.4.cs:line 0 at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
Boolean& completedSynchronously)
如果我在application_error方法中尝试任何代码,例如重定向,仍然是错误页面:
错误101(net :: ERR_CONNECTION_RESET):连接已重置.
被展示.
问题
>如何处理此错误?可以事先处理吗?那么这个错误没有显示出来? (我尝试使用jquery来获取文件大小并检查它,但我发现它太复杂了
>如果问题1不是’回答’,是否有办法拦截此错误并显示友好错误?
解决方法
在web.config中的系统Web下
添加此行..
<system.web> <httpRuntime executionTimeout="999" maxRequestLength="2097151"/>
然后你需要检查文件大小
if (AsyncFileUpload1.HasFile) { string FileName = Path.GetFileName(AsyncFileUpload1.PostedFile.FileName); string Extension = Path.GetExtension(AsyncFileUpload1.PostedFile.FileName); string FolderPath = ConfigurationManager.AppSettings["FolderPath"]; string FilePath = Server.MapPath("~/xl/" + FileName); double filesize = (double)AsyncFileUpload1.FileBytes.Length; if (filesize < 106496) { //do something } else { Response.Write("File size must be less than 2MB."); }
如果您发现它有用,请将其标记为您的答案,否则请告诉我.