# ASP.NET 的 Web.Config 屬性:maxRequestLength、maxAllowedContentLength、maxJsonLength(檔案上傳大小 & JSON 長度) ###### tags: `ASP.NET` `C#` `JSON` ## 大綱 本篇記錄 Web.Config 以下屬性用法: 1. maxRequestLength(檔案上傳大小限制) 2. maxAllowedContentLength(檔案上傳大小限制) 3. maxJsonLength(JSON 字串長度限制) ## maxRequestLength * 作用:檔案上傳大小限制 * maxRequestLength: (1) 單位:KB (2) 預設值:4096 KB(4 MB) (3) 最大值:2147483647 KB (註:maxRequestLength 型別為 int,所以理論上、其最大值為 int 上限 2147483647。但因為檔案上傳大小限制還受到 IIS 與 maxAllowedContentLength 屬性影響,故實際無法上傳這麼大的檔案) * executionTimeout (1) 單位:秒 * 以下範例將 maxRequestLength 設定為 100MB(102400KB),executionTimeout 為 600 秒(10 分鐘): ``` XML <system.web> <httpRuntime maxRequestLength="102400" executionTimeout="600"/> </system.web> ``` ## maxAllowedContentLength * 作用:檔案上傳大小限制 * 單位:byte * 預設值:30000000 byte(大約 28.6 MB) * 最大值:4294967295 byte(大約 4 GB) * 以下範例將 maxAllowedContentLength 設定為 100MB(1024 x 1024 x 100 = 104857600): ``` XML <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="104857600" /> </requestFiltering> </security> </system.webServer> ``` ## maxJsonLength * 作用:JSON 字串長度限制 * 單位:byte * 預設值:102400 byte(100 KB) * 最大值:其型別為 int,所以最大值為 int 上限 2147483647 byte(2 GB)。 * 以下範例將 maxJsonLength 設定為 50000000 byte(大約 47 MB): ``` XML <configuration> <system.web.extensions> <scripting> <webServices> <jsonSerialization maxJsonLength="50000000"/> </webServices> </scripting> </system.web.extensions> </configuration> ``` ## 統整表 |屬性|單位|預設值|最大值| |---|---|-----|-----| |maxRequestLength |KB |4096 KB(4 MB) | 2147483647 KB| |maxAllowedContentLength|byte|30000000 byte(大約為 28 MB)|4294967295 byte(大約為 4 GB)| |maxJsonLength |byte|102400 byte(100 KB) |2147483647 byte(2 GB)| ## 參考資料 * Maximum value of maxRequestLength? https://stackoverflow.com/questions/4548305/maximum-value-of-maxrequestlength * [ASP.NET] File Upload 上傳檔案大小的限制 https://dotblogs.com.tw/shadow/2017/09/15/130258 * Can I set an unlimited length for maxJsonLength in web.config? https://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config * Confusion over maxJsonLength default value https://stackoverflow.com/questions/2119186/confusion-over-maxjsonlength-default-value
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up