After traversing all the posts about uploading large files in csdn, I wrote this spam. (:-))
There are several ways to upload large files:
1. The thought HttpWorkerRequest method is too difficult to understand :-(
2. Use the third-party control AspNetUpload for money !! Forget it. We still like free.
3. Modify the web. config file, but the error cannot be captured.
4. Upload through ftp. The server must provide the ftp service.
No. You have to choose the third method. Who made me stupid! (It's too stupid. It doesn't matter. If you want to eat more fish, I heard that fish can be smarter .)
So study the third method carefully!
Modify the Webcong file:
<System. web>
& Lt; httpRuntime maxRequestLength = "40690"
UseFullyQualifiedRedirectUrl = "true"
ExecutionTimeout = "6000"
UseFullyQualifiedRedirectUrl = "false"
MinFreeThreads = "8"
MinLocalRequestFreeThreads = "4"
AppRequestQueueLimit = "100"
EnableVersionHeader = "true"
/>
</System. web>
Which is closely related to upload:
MaxRequestLength
Indicates the maximum file upload size supported by ASP. NET.
This restriction can be used to prevent DoS attacks caused by a large number of files being transferred to the server.
The specified size is in KB.
The default value is 4096 KB (4 MB ).
ExecutionTimeout
Indicates the maximum number of seconds allowed to execute a request before it is automatically disabled by ASP. NET.
The Unit is seconds. When uploading a large file, this setting is larger.
If the server memory is 512 MB, files with a size of MB can be uploaded. (I have never tried it. Comments from all the posts on csdn .)
The web. config settings are now complete.
However, once the size of the uploaded file exceeds the configured file size range, the following error occurs:
This page cannot be displayed
The page you want to view is currently unavailable. The website may encounter technical problems, or you need to adjust the browser settings.
If this problem cannot be solved, capture the error! What should we do?
I have eaten a few fish recently and thought about it. Because this error is caused by a file control foreground error, it is not feasible to use try... catch in the background.
So I thought of using the. NET error capture page mechanism for processing. Feasible.
1. Set web. config first
<CustomErrors mode = "On"/>
2. Create an error. aspx file to capture errors.
3. Add the page command on the front-end page of The aspx page of the uploaded file. ErrorPage = "UploadError. aspx"
4. Add some code in error. aspx to determine whether the error message is a file-caused foreground error.
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
{
Response. Redirect ("uploadexcel. aspx? Err = 1 "); // jump to the upload page again,
The err parameter is added to display the error message.
}
}
5. An error message is displayed.
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. Close the job. (This fish is not white)
After the above modification, the current upload is changed to the file size specified by the program. If the file size exceeds, an error message is displayed.
In this way, it is much better. At least I can accept it myself, and it is not difficult to explain it to users.