Capture downloads to large files

Asp.Net has an upper limit for downloading files. I am trying to catch this server side situation. According to the documentation I found, it should be possible to override Application_Error in Global.asax, but this does not work for me. The second option is to override OnError on the receiving page, but this also does not work.

Can someone show some working code on how to catch this error on the server side?

+3
source share
5 answers

Put the following in Golobal.asax.cs:

void Application_Error(Object sender, EventArgs e)
        {
            HttpException ex = Server.GetLastError() as HttpException;
            if (ex != null)
            {
                if ((ex.GetHttpCode() == 500 || ex.GetHttpCode() == 400) && ex.ErrorCode == -2147467259)
                {
                    Server.ClearError();
                    Response.Redirect("~/MaximumFileError.aspx", false);
                }
            }
        }

This worked for me, but I'm not sure if it works for all cases.

+2
source

Uploadify - jquery flash uploader, . , , .

+1

, , , web.config? :

System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
double maxFileSize = section.MaxRequestLength;
0

"- - ?"

. , , .

, , - , .

0

maxRequestLength. , , . DoS Attack. web.config maxRequestLength 8 :

 <httpRuntime maxRequestLength="8192" executionTimeout="3600" />

, maxRequestLength, , , max RequestLength, .config, . Global.asax. , , , System.Web.HttpUnhandledException ! , , , , , .

void Application_Error(object sender, EventArgs e){  
Exception wyjatek = Server.GetLastError();
        if (wyjatek.InnerException != null && wyjatek.InnerException.Message.Contains("Maximum request length exceeded"))
        {
            Server.ClearError();
            Response.Redirect("FormWithFile.aspx?alert=Za-duzy-plik");
        }  
    }

Global.asax , ( GET).

ASPX:

MaxRequestLength web.config :

static System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
static HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
int maxFileSize = (section.MaxRequestLength/2)*1024; 

, , :

  protected void InsertButton_Click(object sender, EventArgs e)
        {
            if (((FileUpload)FormView1.FindControl("FileUpload1")).HasFile) // WHEN USER WANT TO UPLOAD FORM WITH FILE (its, optional)
            {
                HttpPostedFile file = (HttpPostedFile)(((FileUpload)FormView1.FindControl("FileUpload1")).PostedFile);
                int iFileSize = file.ContentLength;
                if ((file != null) && (file.ContentLength > 0))
                {
                   if (iFileSize > maxFileSize) // checking image SIZE!
                    { 
                        MessageForUser.Text = "<h1 class=error>Bad File! File is to big!</h1>";
                    }
                    else
                    {
                        byte[] plik = ((FileUpload)FormView1.FindControl("FileUpload1")).FileBytes;
              // HERE COMES CODE FOR INSERT OF FORM WITH FILE
                        MessageForUser.Text = "<h1>Insert was sucessfull</h2>";
                    }
                }
            }
            else
            {
              // HERE COMES CODE FOR INSERT OF FORM WITHOUT FILE
                MessageForUser.Text = "<h1>Insert was sucessfull</h2>";
        }
    }

Page_Load , , GET Global.asax, , .

 if (Request.QueryString["alert"]!=null)
            {
                string temp =  Request.QueryString["alert"].Replace('-',' ');
                MessageForUser.Visible = true;
                MessageForUser.Text = "<h1 class=error>ERROR: " + temp + "</h1>";
            }   

, , :

  • , DoS- 8 , , .
  • Global.asax, .
  • , - .
  • Temporarily even more than 8 MB files come to the server, but those that manage in executeTimeout

Possible alternatives:

  • Use flash technology to check client-side file size
  • Use some flow methods to pass bites into small packets, and the moment you reach the threshold, throw your own exception and handle it. suitable reading: Sending a file to HttpHandler chunks
0
source

Source: https://habr.com/ru/post/1724085/


All Articles