사용자가 Web.config에서 사이트에 업로드 할 수있는 파일 크기가 제한됩니다. 설명 된대로 here, 크기가 받아 들여지지 않으면 ConfigurationErrorsException이 throw됩니다. 나는 업로드 요청을 위해 액션 메소드 나 컨트롤러에서 그것을 잡으려고했지만 행운은 없었다. 연결이 재설정되었으므로 오류 페이지가 표시되지 않습니다.maxRequestLength 위반에 대한 ConfigurationErrorsException을 잡는 방법?
BeginRequest 이벤트에서 catch하려고했지만 예외가 처리되지 않아도 처리되지 않았습니다. 여기에 코드입니다 :
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
try
{
if (context.Request.ContentLength > maxRequestLength)
{
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
// Check if body contains data
if (workerRequest.HasEntityBody())
{
// get the total body length
int requestLength = workerRequest.GetTotalEntityBodyLength();
// Get the initial bytes loaded
int initialBytes = 0;
if (workerRequest.GetPreloadedEntityBody() != null)
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
if (!workerRequest.IsEntireEntityBodyIsPreloaded())
{
byte[] buffer = new byte[512];
// Set the received bytes to initial bytes before start reading
int receivedBytes = initialBytes;
while (requestLength - receivedBytes >= initialBytes)
{
// Read another set of bytes
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
// Update the received bytes
receivedBytes += initialBytes;
}
initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
}
}
}
}
catch(HttpException)
{
context.Response.Redirect(this.Request.Url.LocalPath + "?action=exception");
}
}
하지만 난 아직이 얻을 :
Maximum request length exceeded.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Maximum request length exceeded.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
업데이트 :
어떤 방법 어쨌든 예외가 발생? 요청을 읽으면 예외가 발생합니다. 전혀 읽지 않으면 브라우저에서 "101 Connection Reset"이 표시됩니다. 여기서 무엇을 할 수 있습니까? 액션 메소드에서
, 내가 봤이 발견 : http://stackoverflow.com을/questions/2966076/getting-file-size-in-javascript. 이 주제에 따르면 일반 Javascript로 파일 크기를 알아낼 방법이 없습니다 (Flash, ActiveX 또는 HTML 5를 사용하고 싶지 않습니다!). 정확하게 이해하고 있습니까? – celticharp
@necronio 그렇게 생각합니다. –