在.net中默认上传文件大小是4m,现在我想当它上传的文件大小超过4m时给用户一个友好界面,我试了几次好像多不行啊,捕捉不到异常,我使过用这种方法
//private void Application_BeginRequest(Object source, EventArgs e)
//{
// HttpRequest request = HttpContext.Current.Request;
// if (request.ContentLength > 40690)
// {
// HttpApplication app = source as HttpApplication;
// HttpContext context = app.Context;
// HttpWorkerRequest wr =
// (HttpWorkerRequest)(context.GetType().GetProperty("WorkerRequest ", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(context,null));
byte[] buffer;
if (wr.HasEntityBody())
{
int contentlen = Convert.ToInt32(wr.GetKnownRequestHeader(
HttpWorkerRequest.HeaderContentLength));
buffer = wr.GetPreloadedEntityBody();
int received = buffer.Length;
int totalrecv = received;
if (!wr.IsEntireEntityBodyIsPreloaded())
{
buffer = new byte[65535];
while((contentlen- totalrecv)>=received)
{
received = wr.ReadEntityBody(buffer,
buffer.Length);
totalrecv += received;
received =
wr.ReadEntityBody(buffer, contentlen - totalrecv);
}
}
}
context.Response.Redirect( "../Default.aspx ");
}
}
这段代码写在Global.asax文件里面,这里我不太明白 (HttpWorkerRequest)(context.GetType().GetProperty("WorkerRequest ", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(context,null));这段代码中“WorkerRequest”这个参数的含义,希望那个大家帮帮忙,或者提供更好的办法!!!!!!!!
没遇到过这样的问题
相当于HttpWorkerRequest wr = Context.WorkerRequest。WorkerRequest 是个私有变量,只能通过反射获取。这样是不使用客户端控件的情况下,最好的处理方法(客户端可以有flash,firefox中的file对象)。另外虽然可以通过Web.config调整上存文件的大小,但是这样并不能显示友好界面。因为文件一旦超出设定大小,IIS就会拒绝,出现拒绝服务的界面,从而不能热direct到想要的页面。