asp.net - error handing for uploading large size file -
how can send message user if file uploaded exceeds maximum limit on server side using asp.net. have wriiten following code in global.asax
sub application_error(byval sender object, byval e eventargs) dim ex new exception ex = server.getlasterror() if typeof ex httpunhandledexception andalso ex.innerexception isnot nothing ex = ex.innerexception end if if (ex isnot nothing) , ex.message.contains("maximum request length exceeded") server.clearerror() server.transfer("/errors.aspx") end if end sub
it not display erros page instead internet explorer cannot display webpage
that's late in cycle. test in application_beginrequest this:
sub application_beginrequest(byval sender object, byval e eventargs) dim runtime httpruntimesection = ctype(webconfigurationmanager.getsection("system.web/httpruntime"), httpruntimesection) ' approx 100 kb(for page content) size has been deducted because maxrequestlength proprty ' page size, not file upload size dim maxrequestlength integer = (runtime.maxrequestlength - 100) * 1024 ' code used check request length of page , if request length ' greater maxrequestlength return same page query string value action=exception dim mycontext httpcontext = ctype(sender, httpapplication).context if mycontext.request.contentlength > maxrequestlength ' need consume file otherwise browser detect upload did not complete ' , assume connection broken , display "page not found" error dim pro iserviceprovider = ctype(mycontext, iserviceprovider) dim workerrequest httpworkerrequest = directcast(pro.getservice(gettype(httpworkerrequest)), httpworkerrequest) if workerrequest.hasentitybody dim requestlength integer = workerrequest.gettotalentitybodylength dim initialbytes integer = 0 if workerrequest.getpreloadedentitybody isnot nothing initialbytes = workerrequest.getpreloadedentitybody.length end if if not workerrequest.isentireentitybodyispreloaded dim buffer byte() = new byte(511999) {} dim receivedbytes integer = initialbytes while (requestlength - receivedbytes) >= initialbytes initialbytes = workerrequest.readentitybody(buffer, buffer.length) receivedbytes += initialbytes end while initialbytes = workerrequest.readentitybody(buffer, requestlength - receivedbytes) end if response.redirect("~/utility/errormaxlength.htm") end if end if end sub
source http://weblogs.asp.net/hosamkamel/resolving-maximum-request-length-exceeded-exception
Comments
Post a Comment