After going through all the posts about uploading large files in csdn, I wrote this piece of garbage. (:-))
There are several ways to upload large files:
1. Sigui’s HttpWorkerRequest method is too difficult to understand:-(
2. Use the third-party control AspNetUpload to ask for money! ! Forget it, we still like free.
3. Modify the web.config file, but cannot catch errors.
4. Upload via ftp. The server needs to provide ftp service.
We have no choice but to choose the third way. Who makes us so stupid! (It’s too stupid. It doesn’t matter. You should eat more fish. I heard that eating fish can make you smarter.)
Then study the third method carefully!
Modify Webcong file:
<system.web>
<httpRuntime maxRequestLength="40690"
useFullyQualifiedRedirectUrl="true"
executionTimeout="6000"
useFullyQualifiedRedirectUrl="false"
minFreeThreads="8"
minLocalRequestFreeThreads="4"
appRequestQueueLimit="100"
enableVersionHeader="true"
/>
</system.web>
Among them, those closely related to uploading are:
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 the server.
The specified size is in KB.
The 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 to a larger value 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.)
'www.downcodes.com
At this point, the settings of web.config have ended.
However, once the size of the uploaded file exceeds the set file size range, 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! What to do?
I ate a few fish recently and thought about it. Since this error is a foreground error caused by the file control, it is not feasible to use try...catch to catch it in the background.
So I thought of using the error capture page mechanism of .NET to handle it. It's doable.
1. Set up web.config first
<customErrors mode="On"/>
2. Create a new error.aspx file specifically for catching errors.
3. Add the page directive to the front page of the aspx page where the file is uploaded. ErrorPage="UploadError.aspx"
4. Add some code to 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 //Foreground error ex is null value
{
Response.Redirect("uploadexcel.aspx?err=1"); //Rejump to the upload page and add the err parameter to display the error message
}
}
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('Upload file has failed ! File size is too large !')</script>");
}
}
}
6. Get it done and call it a day. (This fish is not free.)
After the above modification, the upload now becomes that the program specifies the file size. If it exceeds, an error message will pop up.
In this way, it will be much better. At least I can accept it, and it will not be too hard to explain to users.