2012-10-11 23 views
2

我將此添加到我的web.config文件:如何限制文件上傳的大小但顯示有意義的響應?

<httpRuntime maxRequestLength="40960"/> <!-- Limit to 40 megabytes --> 

,當我嘗試上載的東西超過40兆大,我得到這個在Firefox:

The connection was reset 

The connection to the server was reset while the page was loading. 

    * The site could be temporarily unavailable or too busy. Try again in a few 
    moments. 
    * If you are unable to load any pages, check your computer's network 
    connection. 
    * If your computer or network is protected by a firewall or proxy, make sure 
    that Firefox is permitted to access the Web. 

有一些方法也從中受益這內置的文件大小限制,但實際上顯示一個不錯的錯誤頁面?當然,我可以在控制器中執行此檢查,但此時流量已耗盡,因爲大文件已到達我的服務器。

if (image.ContentLength > 40960) // 'image' is HttpPostedFileBase 

有什麼建議嗎?

回答

3

這在你的global.asax.cs中怎麼樣?

void Application_Error(object sender, EventArgs e) 
{ 
    if (Request.TotalBytes > 40960 * 1024) 
    { 
     Server.ClearError(); 
     Response.Clear(); 
     Response.Write("File uploaded is greater than 40 MB"); 
    } 

} 
相關問題