asp.net程序默認上傳文件是有大小限制的,默認是4M。IIS7下默認是30M。因此需要配置幾個項目進行支持大文件(最大2G)的上傳。
1、配置錯誤
說明: 在處理向該請求提供服務所需的配置文件時出錯。請檢查下面的特定錯誤詳細信息並適當地修改配置文件。
分析器錯誤消息: 屬性“maxRequestLength”的值無效。錯誤為: 該值必須在 0-2097151 范圍內。
如果出現以下錯誤:
說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆棧跟蹤信息,以了解有關該錯誤以及代碼中導致錯誤的出處的詳細信息。
異常詳細信息: System.Web.HttpException: 超過了最大請求長度。 |
那是因為配置的maxRequestLength超過了2097151。
解決辦法1:
Web.config 配置:
<configuration><system.web><httpRuntime maxRequestLength="4096" executionTimeout="3600" /></system.web><configuration>
注意修改超時時間:executionTimeout.單位是:秒。
解決辦法2:修改Global.asax.cs的Application_BeginRequest方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
Using System.Web.Configuration;
void
Application_BeginRequest(
object
sender, EventArgs e)
{
//本代碼的功能是檢查頁面請求的大小,如果超過了配置文件maxRequestLength的設定值,就提示用戶超過了所允許的文件大小。
//從配置文件里得到配置的允許上傳的文件大小
HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection(
"system.web/httpRuntime"
);
//maxRequestLength 為整個頁面的大小,不僅僅是上傳文件的大小,所以扣除 100KB 的大小,
//maxRequestLength單位為KB
int
maxRequestLength = (runTime.MaxRequestLength) * 1024;
//當前請求上下文的HttpApplication實例
//HttpContext context = ((HttpApplication)sender).Context;
//判斷請求的內容長度是否超過了設置的字節數
if
(Request.ContentLength > maxRequestLength)
{
#region 不理解這些代碼存在的意義
/*
//得到服務對象
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
//檢查請求是否包含正文數據
if (workerRequest.HasEntityBody())
{
//請求正文數據的長度
int requestLength = workerRequest.GetTotalEntityBodyLength();
//得到加載的初始字節數
int initialBytes = 0;
if (workerRequest.GetPreloadedEntityBody() != null)
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
//檢查是否所有請求數據可用
if (!workerRequest.IsEntireEntityBodyIsPreloaded())
{
byte[] buffer = new byte[512000];
//設置要接收的字節數為初始字節數
int receivedBytes = initialBytes;
//讀取數據,並把所有讀取的字節數加起來,判斷總的大小
while (requestLength - receivedBytes >= initialBytes)
{
//讀取下一塊字節
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
//更新接收到的字節數
receivedBytes += initialBytes;
}
initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
}
}
*/
#endregion
//注意這里可以跳轉,可以直接終止;在VS里調試時候得不到想要的結果,通過IIS才能得到想要的結果;FW4.0經典或集成都沒問題
htm = htm&(
"請求大小"
+ Request.ContentLength);
Response.End();
}
}
|
2、問題:HTTP 錯誤 404.13 - Not Found
HTTP 錯誤 404.13 - Not Found 請求篩選模塊被配置為拒絕超過請求內容長度的請求。 最可能的原因: •Web 服務器上的請求篩選被配置為拒絕該請求,因為內容長度超過配置的值。 可嘗試的操作: •確認 applicationhost.config 或 web.config 文件中的 configuration/system.webServer/security/requestFiltering/requestLimits@maxAllowedContentLength 設置。 詳細錯誤信息: 模塊 RequestFilteringModule
通知 BeginRequest
處理程序 PageHandlerFactory-Integrated
錯誤代碼 0x00000000 |
原因:Web 服務器上的請求篩選被配置為拒絕該請求,因為內容長度超過配置的值(IIS 7 默認文件上傳大小時30M)。
解決:更改asp.net文件上傳大小限制
修改IIS的applicationhost.config
文件位置: %windir%/system32/inetsrv/config/applicationhost.config
找到<requestFiltering>節點,該節點下默認沒有 <requestLimits maxAllowedContentLength="上傳大小的值(單位:byte)" /> 元素。為這個節點添加如下元素:<requestLimits maxAllowedContentLength="2147483647" /> (上傳的大小將改為2G)
web.config中,添加如下內容
<configuration>
<system.web>
<httpRuntime maxRequestLength="2097151" executionTimeout="120"/>
</system.web>
</configuration>
說明:
httpRuntime 配置 ASP.NET HTTP 運行時設置,以確定如何處理對 ASP.NET 應用程序的請求。
maxRequestLength (指示 ASP.NET 支持的最大文件上載大小)
指定輸入流緩沖閾值限制(以 KB 為單位)。此限制可用於防止拒絕服務攻擊;例如,因用戶向服務器發送大型文件而導致的拒絕服務攻擊。
默認值為 4096 (4 MB),最大值只能是2097151K。
executionTimeout
指定在被 ASP.NET 自動關閉前,允許執行請求的最大秒數。默認90秒。
只有當 compilation 元素中的調試屬性為 False 時,此超時屬性才適用。若要幫助避免在調試期間關閉應用程序,請不要將此超時屬性設置為較大值。
web.config中,把以下內容加在<system.webServer>節點
<security>
<requestFiltering >
<requestLimits maxAllowedContentLength="2147483647" ></requestLimits>
</requestFiltering>
</security>
上述中maxAllowedContentLengt是以BK為單位。