When I upload an image I had this error:
开发者_JAVA技巧maximum request length exceeded
How can I fix this problem?
Add the following to your web.config file:
<configuration>
<system.web>
<httpRuntime maxRequestLength ="2097151"/>
</system.web>
</configuration>
This sets it to 2GB. Not certain what the max is.
You can increase the maximum length of requests in web.config, under <system.web>
:
<httpRuntime maxRequestLength="100000" />
This example sets the maximum size to 100 MB.
That's not a terrific way to do it as you're basically opening up your server to DoS attacks allowing users to submit immense files. If you know that the user should only be uploading images of a certain size, you should be enforcing that rather than opening up the server to even larger submissions.
To do that you can use the example below.
As I was been moaned at for posting a link, I've added what I ultimately implemented using what I learned from the link I previously posted - and this has been tested and works on my own site...it assumes a default limit of 4 MB. You can either implement something like this, or alternatively employ some sort of third-party ActiveX control.
Note that in this case I redirect the user to the error page if their submission is too large, but there's nothing stopping you from customising this logic further if you so desired.
I hope it's useful.
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);
}
}
To avoid maximum request length exceeded,dont upload large size image OR reduce image size between 100kb to 200kb .
精彩评论