Home  >  Article  >  Backend Development  >  How to use .NET's file upload control to modify the web.config file to upload large files

How to use .NET's file upload control to modify the web.config file to upload large files

高洛峰
高洛峰Original
2016-12-24 13:01:441525browse

Modify the Webcong file:

The following is the quoted content:

<system.web> 
<httpRuntime maxRequestLength="40690" 
useFullyQualifiedRedirectUrl="true" 
executionTimeout="6000" 
useFullyQualifiedRedirectUrl="false" 
minFreeThreads="8" 
minLocalRequestFreeThreads="4" 
appRequestQueueLimit="100" 
enableVersionHeader="true" 
/> 
</system.web>

The one closely related to uploading is:

maxRequestLength

Indicates the maximum file upload size supported by ASP.NET.

This limit can be used to prevent denial of service attacks caused by users passing large numbers of files to this server.

The specified size is in KB.

Default value is 4096 KB (4 MB).

executionTimeout

Indicates the maximum number of seconds a request is allowed to execute before being automatically closed by ASP.NET.

The unit is seconds, set this larger when uploading large files.

If the server memory is 512M, files of 160M can be uploaded. (I haven’t tried it. This is the unanimous opinion of many posts on csdn.)

The setting of web.config is over here.

But once the size of the uploaded file exceeds the file size range of this setting, the following error will occur: The page cannot be displayed The page you want to view is currently unavailable. The website may be experiencing technical difficulties, or you may need to adjust your browser settings.

Although it can’t be solved, we still need to catch this error! How to do it? Since this error is a foreground error caused by the file control, trying to catch it using try...catch in the background will not work.

So I thought of using the error capture page mechanism of .NET to handle it. It's doable.

The following is the quoted content:
1. Set up web.config first

<customErrors mode="On"/>

2. Create a new error.aspx file specifically for catching errors.

3. Add the page command to the front page of the aspx page where the file is uploaded. ErrorPage="UploadError.aspx"

4. Add some code in error.aspx to determine whether the error message is a foreground error caused by file.

public class UploadError : System.Web.UI.Page 
{ 
private void Page_Load(object sender, System.EventArgs e) 
{ 
Exception ex = Server.GetLastError(); 
if (ex != null) 
{ 
Response.Redirect("../error.aspx"); 
} 
else //前台错误ex为空值 
{ 
Response.Redirect("uploadexcel.aspx?err=1"); //重新跳转到上传页面, 
加上err参数是为了显示错误信息 
} 
}

5. Display error message.

public class uploadexcel : System.Web.UI.Page 
{ 
private void Page_Load(object sender, System.EventArgs e) 
{ 
if (Request["err"] == "1") 
{ 
Page.RegisterStartupScript("budget","<script language = javascript> 
alert(&#39;Upload file has failed ! File size is too large !&#39;)</script>"); 
} 
} 
}


More on how to use the .NET file upload control. Modify the web.config file to upload large files. For related articles, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn