开发者

redirect to error on exceeding fileupload size

I am trying to direct user to a error page when maximum fileupload size excedds.For this iam doing the following thing:

public void Init(HttpApplication application)
    {
        application.BeginRequest += new EventHandler(application_BeginRequest);
    }

    void application_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;
        if (context.Request.ContentLength > 4096000)
        {
            context.Response.Redirect("~/Error.aspx");
        }
    }
    public void Dispose()
    {
    }
开发者_如何学JAVA

But on uploading file with size more than the max filesize it is showing "connection reset" error. How should i do this.


Check this question: Display custom error page when file upload exceeds allowed size in ASP.NET MVC


The ASP.NET runtime might reject the request even before your application fires up. Check the maximum request length set in a web.config - the value should be greater then the one your application is expecting:

IIS6/IIS7 (classic pipeline AND integrated pipeline):

<system.web>
  <!-- The value is in KB -->
  <httpRuntime maxRequestLength="4096" />
</system.web>

IIS7 (integrated pipeline):

<system.webServer> 
  <security> 
    <requestFiltering> 
      <!-- The value is in bytes -->
      <requestLimits maxAllowedContentLength="209715200" />
    </requestFiltering>
  </security>
</system.webServer>

Hope this will help.

-- Pavel


Okay, you're probably getting a ThreadAbortException, that's why the connection is reset.

Please refer to this KB article for a workaround: ThreadAbortException Occurs If You Use Response.End, Response.Redirect, or Server.Transfer.

-- Pavel


I believe that to do this correctly from the handler you will need to read all of the data off of the request before sending the response/redirect. Alternatively, you can do this from an ISAPI filter and avoid having to read the data.


You can use toolkit AsyncFileUpload control and then catch the error in the client side and redirect to the error page.

AsyncFileUpload: How do I hide the max request length exceeded alert error?

<asp:AsyncFileUpload ID="fileUpload" 
                     runat="server" OnClientUploadError="onUploadError"
                     OnClientUploadComplete="uploadComplete"
                     OnUploadedComplete="OnUploadComplete" />

<script language="javascript" type="text/javascript">

     function onUploadError()
     {
          //Redirect to error page.
     }

     function uploadComplete()
     {

     }


 </script>


Use Transfer.Redirect()


Following the post the reason can be just a redirection to local site:

It doesn't seem to work when the server is local (either through using localhost, local ip or http://machinename/). The browser just reacts like the server completely dropped the connection - connection reset by peer / connection was reset (FF) / cannot display web page (IE).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜