云和山的彼端

江南好, 风景旧曾谙。日出江花红胜火,春来江水绿如蓝,能不忆江南。

截获asp.net上传文件过大IIS报错的Httpmodule代码 --方便以后使用

public class UpLoadHttpModule : IHttpModule
{
    
// Methods
    private void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpRequest request 
= HttpContext.Current.Request;
        
if (request.ContentLength > 0x400000)
        {
            HttpApplication app 
= sender as HttpApplication;
            HttpContext context 
= app.Context;
            
try
            {
                HttpWorkerRequest wr 
= (HttpWorkerRequest) context.GetType().GetProperty("WorkerRequest", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(context, null);
                
if (wr.HasEntityBody())
                {
                    
int contentlen = Convert.ToInt32(wr.GetKnownRequestHeader(11));
                    
byte[] buffer = wr.GetPreloadedEntityBody();
                    
int received = 0;
                    
if (buffer != null)
                    {
                        received 
= buffer.Length;
                    }
                    
int totalrecv = received;
                    
if (!wr.IsEntireEntityBodyIsPreloaded())
                    {
                        buffer 
= new byte[0xffff];
                        
while ((contentlen - totalrecv) >= received)
                        {
                            received 
= wr.ReadEntityBody(buffer, buffer.Length);
                            totalrecv 
+= received;
                        }
                        received 
= wr.ReadEntityBody(buffer, contentlen - totalrecv);
                    }
                }
                context.Response.Redirect(
"error.aspx" + request.Url.Query + "");
            }
            
catch (Exception)
            {
                context.Response.Redirect(
"error.aspx" + request.Url.Query + "");
            }
        }
    }

    
private void application_EndRequest(object sender, EventArgs e)
    {
    }
    
public void Dispose()
    {
    }
    
public void Init(HttpApplication application)
    {
        application.BeginRequest 
+= new EventHandler(this.Application_BeginRequest);
        application.EndRequest 
+= new EventHandler(this.application_EndRequest);
    }
}

posted on 2007-11-13 10:51 jecray 阅读(224) 评论(0)  编辑 收藏 网摘 所属分类: .NET Framework


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索
[推荐职位]上海盛大网络招聘架构师



China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》

相关文章:

相关链接: