2010-09-08 9 views
20

मैं एक छवि मैं इस त्रुटि थी अपलोड करते हैं:मैं "अधिकतम अनुरोध लंबाई पार हो गया" अपवाद कैसे हल करूं?

maximum request length exceeded

मैं कैसे इस समस्या को ठीक कर सकते हैं?

+1

की संभावित डुप्लिकेट [अधिकतम अनुरोध लंबाई से अधिक] (http://stackoverflow.com/questions/3853767/maximum-request-length-exceeded) – niico

+0

तरह niico कहा, उदाहरण के लिए, यदि आप का उपयोग करें Web.config में एक से अधिक पंक्तियों में, आपको वह त्रुटि मिल सकती है। – oguzhan

उत्तर

41

अपने web.config फ़ाइल में निम्न जोड़ें:

<configuration> 
    <system.web> 
     <httpRuntime maxRequestLength ="2097151"/> 
    </system.web> 
</configuration> 

यह 2GB करने के लिए सेट करता है। निश्चित नहीं है कि अधिकतम क्या है।

+3

सुनिश्चित करें कि आप इसे 'Views' फ़ोल्डर के अंदर एक के बजाय मुख्य' Web.config' में जोड़ रहे हैं ... –

+3

हाँ, लेकिन इस अपवाद को कैसे पकड़ें? MaxRequestLength को 2 जीबी तक सेट करना मुझे लगता है कि सबसे अच्छा नहीं है choise .... – MDDDC

+0

मेरा दिन बचाया! धन्यवाद दोस्त! – Flappy

20

आप <system.web> के तहत, web.config में अनुरोधों की अधिकतम लंबाई को बढ़ा सकते हैं:

<httpRuntime maxRequestLength="100000" /> 

यह उदाहरण 100   एमबी करने के लिए अधिकतम आकार तय करता है।

7

यह ऐसा करने का एक शानदार तरीका नहीं है क्योंकि आप मूल रूप से DoS attacks पर अपने सर्वर को खोल रहे हैं ताकि उपयोगकर्ता अत्यधिक फाइलें जमा कर सकें। यदि आप जानते हैं कि उपयोगकर्ता को केवल एक निश्चित आकार की छवियों को अपलोड करना चाहिए, तो आपको सर्वर को भी बड़े सबमिशन तक खोलने के बजाय लागू करना चाहिए।

ऐसा करने के लिए आप नीचे दिए गए उदाहरण का उपयोग कर सकते हैं।

जैसा कि मुझे एक लिंक पोस्ट करने के लिए moaned किया गया था, मैंने जो जोड़ा मैंने अंततः पोस्ट किए गए लिंक से सीखा है उसका उपयोग करके मैंने जो कुछ भी सीखा है, उसका उपयोग किया है - और यह परीक्षण किया गया है और मेरी अपनी साइट पर काम करता है ... यह मानता है 4   एमबी की डिफ़ॉल्ट सीमा। आप या तो इस तरह कुछ लागू कर सकते हैं, या वैकल्पिक रूप से किसी प्रकार के तृतीय-पक्ष ActiveX नियंत्रण को नियोजित कर सकते हैं।

ध्यान दें कि इस मामले में मैं उपयोगकर्ता को त्रुटि पृष्ठ पर रीडायरेक्ट करता हूं यदि उनका सबमिशन बहुत बड़ा है, लेकिन अगर आप चाहें तो इस तर्क को और अनुकूलित करने से आपको रोक नहीं सकते हैं।

मुझे आशा है कि यह उपयोगी होगा।

public class Global : System.Web.HttpApplication { 
    private static long maxRequestLength = 0; 

    /// <summary> 
    /// Returns the max size of a request, in kB 
    /// </summary> 
    /// <returns></returns> 
    private long getMaxRequestLength() { 

     long requestLength = 4096; // Assume default value 
     HttpRuntimeSection runTime = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection; // check web.config 
     if(runTime != null) { 
      requestLength = runTime.MaxRequestLength; 
     } 
     else { 
      // Not found...check machine.config 
      Configuration cfg = ConfigurationManager.OpenMachineConfiguration(); 
      ConfigurationSection cs = cfg.SectionGroups["system.web"].Sections["httpRuntime"]; 
      if(cs != null) { 
       requestLength = Convert.ToInt64(cs.ElementInformation.Properties["maxRequestLength"].Value); 
      } 
     } 
     return requestLength; 
    } 

    protected void Application_Start(object sender, EventArgs e) { 
     maxRequestLength = getMaxRequestLength(); 
    } 

    protected void Application_End(object sender, EventArgs e) { 

    } 

    protected void Application_Error(object sender, EventArgs e) { 
     Server.Transfer("~/ApplicationError.aspx"); 
    } 

    public override void Init() { 
     this.BeginRequest += new EventHandler(Global_BeginRequest); 
     base.Init(); 
    } 

    protected void Global_BeginRequest(object sender, EventArgs e) { 

     long requestLength = HttpContext.Current.Request.ContentLength/1024; // Returns the request length in bytes, then converted to kB 

     if(requestLength > maxRequestLength) { 
      IServiceProvider provider = (IServiceProvider)HttpContext.Current; 
      HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); 

      // Check if body contains data 
      if(workerRequest.HasEntityBody()) { 

       // Get the total body length 
       int bodyLength = 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(bodyLength - receivedBytes >= initialBytes) { 

         // Read another set of bytes 
         initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length); 

         // Update the received bytes 
         receivedBytes += initialBytes; 
        } 
        initialBytes = workerRequest.ReadEntityBody(buffer, bodyLength - receivedBytes); 
       } 
      } 

      try { 
       throw new HttpException("Request too large"); 
      } 
      catch { 
      } 

      // Redirect the user 
      Server.Transfer("~/ApplicationError.aspx", false); 
     } 
    } 
संबंधित मुद्दे

 संबंधित मुद्दे