none
<httpRuntime maxRequestLength="20480"/> 的问题? RRS feed

  • 问题

  • 如果上传的文件超过了 <httpRuntime maxRequestLength="2048000"/>的设置,会显示一个错误页面出来,怎样让其显示成自定义页面 或者不让其报错呢?谢谢!
    2009年12月22日 8:43

答案

  • 你好:

    参考下面的代码的应该可以解决不报错的问题。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    
    /// <summary>
    /// Summary description for CustomizeHttpModule
    /// </summary>
    namespace Test
    {
        public class CustomizeHttpModule : IHttpModule
        {
            public CustomizeHttpModule()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            public String ModuleName
            {
                get { return "HelloWorldModule"; }
            }
    
            // In the Init function, register for HttpApplication 
            // events by adding your handlers.
            public void Init(HttpApplication application)
            {
                application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    
            }
    
            // Your BeginRequest event handler.
            private void Application_BeginRequest(Object sender, EventArgs e)
            {
                HttpApplication ha = sender as HttpApplication;
                HttpContext context = ha.Context;
    
                if (ha.Context.Request.ContentLength > 1000)//1000 is the max size
                {
                    IServiceProvider provider = (IServiceProvider)context;
                    HttpWorkerRequest wr = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
                    //FileStream fs = null;
                    // Check if body contains data
                    if (wr.HasEntityBody())
                    {
                        // get the total body length
                        int requestLength = wr.GetTotalEntityBodyLength();
                        // Get the initial bytes loaded
                        int initialBytes = 0; //wr.GetPreloadedEntityBody().Length;
    
                        if (!wr.IsEntireEntityBodyIsPreloaded())
                        {
                            byte[] buffer = new byte[100];
                            //string[] fileName = context.Request.QueryString["fileName"].Split(new char[] { '\\' });
                            // fs = new FileStream(context.Server.MapPath("~/Files/temp.pdf"), FileMode.CreateNew);
                            // Set the received bytes to initial bytes before start reading
                            int receivedBytes = initialBytes;
                            while (requestLength - receivedBytes >= initialBytes)
                            {
                                // Read another set of bytes
                                initialBytes = wr.ReadEntityBody(buffer, buffer.Length);
                                // Write the chunks to the physical file
                                // fs.Write(buffer, 0, buffer.Length);
    
                                // Update the received bytes
                                receivedBytes += initialBytes;
                            }
                            initialBytes = wr.ReadEntityBody(buffer, requestLength - receivedBytes);
    
                        }
                    }
                    //fs.Flush();
                    //fs.Close();
                    ha.Context.Response.Redirect("~/Default.aspx");
    
                }
            }
            public void Dispose()
            {
            }
    
        }
    }
    
      <httpModules>
          <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          <add name="TestFileUpLoad" type="Test.CustomizeHttpModule"/>
        </httpModules>



    Microsoft Online Community Support
    • 已标记为答案 KeFang Chen 2009年12月28日 5:53
    2009年12月23日 3:36

全部回复

  • 当超出限制应该会引发HttpException异常

    <customErrors mode="RemoteOnly" defaultRedirect="ErrorPage.aspx"/>
    设置为RemoteOnly 那么远程客户端访问会看到ErrorPage.aspx的信息,本地服务器上看到的是详细的错误信息
    设置为On 无论是远程还是本地服务器查看到的都是自定义信息即 ErrorPage.aspx 这个页面的内容
    设置为Off 那么无论是远程还是本地服务器看到的都是详细的错误信息
    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~
    2009年12月22日 9:32
    版主
  • 你好:

    参考下面的代码的应该可以解决不报错的问题。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    
    /// <summary>
    /// Summary description for CustomizeHttpModule
    /// </summary>
    namespace Test
    {
        public class CustomizeHttpModule : IHttpModule
        {
            public CustomizeHttpModule()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            public String ModuleName
            {
                get { return "HelloWorldModule"; }
            }
    
            // In the Init function, register for HttpApplication 
            // events by adding your handlers.
            public void Init(HttpApplication application)
            {
                application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    
            }
    
            // Your BeginRequest event handler.
            private void Application_BeginRequest(Object sender, EventArgs e)
            {
                HttpApplication ha = sender as HttpApplication;
                HttpContext context = ha.Context;
    
                if (ha.Context.Request.ContentLength > 1000)//1000 is the max size
                {
                    IServiceProvider provider = (IServiceProvider)context;
                    HttpWorkerRequest wr = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
                    //FileStream fs = null;
                    // Check if body contains data
                    if (wr.HasEntityBody())
                    {
                        // get the total body length
                        int requestLength = wr.GetTotalEntityBodyLength();
                        // Get the initial bytes loaded
                        int initialBytes = 0; //wr.GetPreloadedEntityBody().Length;
    
                        if (!wr.IsEntireEntityBodyIsPreloaded())
                        {
                            byte[] buffer = new byte[100];
                            //string[] fileName = context.Request.QueryString["fileName"].Split(new char[] { '\\' });
                            // fs = new FileStream(context.Server.MapPath("~/Files/temp.pdf"), FileMode.CreateNew);
                            // Set the received bytes to initial bytes before start reading
                            int receivedBytes = initialBytes;
                            while (requestLength - receivedBytes >= initialBytes)
                            {
                                // Read another set of bytes
                                initialBytes = wr.ReadEntityBody(buffer, buffer.Length);
                                // Write the chunks to the physical file
                                // fs.Write(buffer, 0, buffer.Length);
    
                                // Update the received bytes
                                receivedBytes += initialBytes;
                            }
                            initialBytes = wr.ReadEntityBody(buffer, requestLength - receivedBytes);
    
                        }
                    }
                    //fs.Flush();
                    //fs.Close();
                    ha.Context.Response.Redirect("~/Default.aspx");
    
                }
            }
            public void Dispose()
            {
            }
    
        }
    }
    
      <httpModules>
          <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          <add name="TestFileUpLoad" type="Test.CustomizeHttpModule"/>
        </httpModules>



    Microsoft Online Community Support
    • 已标记为答案 KeFang Chen 2009年12月28日 5:53
    2009年12月23日 3:36