[ pl4stik @ 24.08.2009. 08:21 ] @
System.Web.HttpException: Maximum request length exceeded.

...se desi kad se uploaduju fileovi veci od definisanog u web.config i to je ok... jel zna neko neko resenje kako da objasnim korisniku da je to zbog velicine dokumenta... Definisao sam u web.configu custom error strane ali kako da proverim jeli korisnik dosao na tu stranu zbog toga sto je pokusao da uploaduje veliki fajl ?
[ deerbeer @ 24.08.2009. 08:58 ] @
Code:
 
 if (fileUpload.PostedFile.ContentLength > 409600) 
{
 // poruka o gresci ... 
}

E sad nadji optimum jer ako je request lenght veci od zadate cifre od one u web configu
onda se handler za upload nece ni pozvati vec ces dobiti onu gresku (Maximum request length exceeded.)
tako da u kodu mozes da smanjis granicu za dozvoljeni limit kako bi korisnik video tu gresku,
ili da je povecas u web.configu.






[ pl4stik @ 24.08.2009. 09:42 ] @
Ne ne ne nismo se razumeli nije meni problem da izmerim PostedFile nego gresku koja se desi pre zavrsenog uploada tj. ovo sto si napisao ne pozivaju mi se uopste kodovi nakon greske ...

Elem, naso sam neko resenje (nije lose) ako ne mislish da ti sve sto imas na strani kad ga posaljes ne predje velicinu koju si definisao u web.config-u, znaci ovo meri maxRequestLength i ako strana posalje vise nego sto je tebi definisano vraca te na stranu sa koje si poslao podatke i u querystringu dobijes action=exception, a to je vec nesto

Ovo ide u Global.asax

Code:
 

   protected void Application_BeginRequest(Object sender, EventArgs e)
        {
        System.Web.Configuration.HttpRuntimeSection runTime = (System.Web.Configuration.HttpRuntimeSection)System.Web.Configuration.WebConfigurationManager.GetSection("system.web/httpRuntime");
        //Approx 100 Kb(for page content) size has been deducted because the maxRequestLength proprty is the page size, not only the file upload size
        int maxRequestLength = (runTime.MaxRequestLength - 100) * 1024;

        //This code is used to check the request length of the page and if the request length is greater than 
        //MaxRequestLength then retrun to the same page with extra query string value action=exception

        HttpContext context = ((HttpApplication)sender).Context;
        if (context.Request.ContentLength > maxRequestLength)
            {
            IServiceProvider provider = (IServiceProvider)context;
            HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));

            // Check if body contains data
            if (workerRequest.HasEntityBody())
                {
                // get the total body length
                int requestLength = workerRequest.GetTotalEntityBodyLength();
                // Get the initial bytes loaded
                int initialBytes = 0;
                if (workerRequest.GetPreloadedEntityBody() != null)
                initialBytes = workerRequest.GetPreloadedEntityBody().Length;
                if (!workerRequest.IsEntireEntityBodyIsPreloaded())
                        {
                        byte[] buffer = new byte[512000];
                        // Set the received bytes to initial bytes before start reading
                        int receivedBytes = initialBytes;
                        while (requestLength - receivedBytes >= initialBytes)
                            {
                            // Read another set of bytes
                            initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);

                            // Update the received bytes
                            receivedBytes += initialBytes;
                            }
                        initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
                        }
                    }
            // Redirect the user to the same page with querystring action=exception. 
            context.Response.Redirect(this.Request.Url.LocalPath + "?action=exception");
            }
        }


Moram josh da testiram, a mogo bi i josh neko ako nema pametnija posla pa josh ako bi napisao zapazanja ovde bilo bi extra, right?