Khi tôi tải lên một hình ảnh tôi đã được lỗi này:Làm cách nào để giải quyết "ngoại lệ yêu cầu tối đa đã vượt quá"?
maximum request length exceeded
Làm thế nào tôi có thể khắc phục vấn đề này?
Khi tôi tải lên một hình ảnh tôi đã được lỗi này:Làm cách nào để giải quyết "ngoại lệ yêu cầu tối đa đã vượt quá"?
maximum request length exceeded
Làm thế nào tôi có thể khắc phục vấn đề này?
Thêm dòng sau vào file web.config của bạn:
<configuration>
<system.web>
<httpRuntime maxRequestLength ="2097151"/>
</system.web>
</configuration>
này đặt nó đến 2GB. Không chắc chắn tối đa là gì.
Bạn có thể tăng độ dài tối đa các yêu cầu trong web.config, dưới <system.web>
:
<httpRuntime maxRequestLength="100000" />
dụ này thiết lập kích thước tối đa đến 100 MB.
Đó không phải là một cách tuyệt vời để làm điều đó khi bạn về cơ bản mở máy chủ của mình lên DoS attacks cho phép người dùng gửi tệp khổng lồ. Nếu bạn biết rằng người dùng chỉ nên tải lên hình ảnh có kích thước nhất định, bạn nên thực thi điều đó thay vì mở máy chủ lên các bản gửi lớn hơn nữa.
Để làm điều đó, bạn có thể sử dụng ví dụ bên dưới.
Khi tôi bị rên rỉ khi đăng liên kết, tôi đã thêm những gì tôi đã thực hiện bằng cách sử dụng những gì tôi đã học được từ liên kết tôi đã đăng - và điều này đã được thử nghiệm và hoạt động trên trang web của riêng tôi ... giới hạn mặc định là 4 MB. Bạn có thể thực hiện một cái gì đó như thế này, hoặc cách khác sử dụng một số loại kiểm soát của bên thứ ba ActiveX. Lưu ý rằng trong trường hợp này tôi chuyển hướng người dùng đến trang lỗi nếu quá trình gửi của họ quá lớn, nhưng không có gì ngăn bạn tùy chỉnh logic này thêm nữa nếu bạn muốn.
Tôi hy vọng nó hữu ích.
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);
}
}
có thể trùng lặp của [chiều dài yêu cầu tối đa vượt quá] (http://stackoverflow.com/questions/3853767/maximum-request-length-exceeded) – niico
Giống như niico nói, ví dụ, nếu bạn sử dụng nhiều hơn một dòng trong Web.config, bạn có thể nhận được lỗi đó. –
oguzhan