Use. NET file control upload files to the final Solution

Source: Internet
Author: User
Tags config file size file upload final ftp server memory
Solution | control | upload

After traversing all the posts about uploading large files in csdn, I wrote this piece of rubbish. (:-))
There are several ways to upload large files:
1, Shi HttpWorkerRequest method, too difficult, can not understand:-(
2, the use of Third-party control aspnetupload want Money!! Forget it, we still like it for free.
3, modify the Web.config file, but can not catch errors.
4, through the way of FTP upload. The server needs to provide an FTP service.

Have to choose a third way, who let us too stupid! (Too stupid, no relationship, to eat more fish, I heard that eating fish can be smart point.) )

Then carefully research the third way!

To modify the Webcong file:
<system.web>
Usefullyqualifiedredirecturl= "true"
executiontimeout= "6000"
Usefullyqualifiedredirecturl= "false"
Minfreethreads= "8"
Minlocalrequestfreethreads= "4"
apprequestqueuelimit= "100"
Enableversionheader= "true"
/>
</system.web>

Which is closely related to the upload is:
maxRequestLength
Indicates the maximum file upload size supported by asp.net.
This restriction can be used to prevent denial of service attacks caused by a user passing a large number of files to the server.
The specified size is in kilobytes.
The default value is 4096 KB (4 MB).

Executiontimeout
Indicates the maximum number of seconds allowed to execute a request before it is automatically closed by asp.net.
The unit is seconds, and when uploading large files, set this up a bit larger.


If the server memory is 512M, you can upload files of size 160M. (haven't tried, csdn the unanimous opinion of the public posts.) )

The setting of the web.config here is over.
However, once the size of the uploaded file exceeds the file size of this setting, the following error occurs:
The page cannot be displayed
The page you are looking for is currently unavailable. Your web site may be experiencing technical problems, or you need to adjust your browser settings.

Although cannot solve, that also must catch this mistake! What do we do?
Recently ate a few fish, think about, because this error is caused by the file control foreground error, so in the background want to use Try...catch to capture is not feasible.
So I thought of using. NET error capture page mechanism to deal with. Yes, it works.

1, first set web.config
<customerrors mode= "on"/>
2, create a new error.aspx file, specifically to catch the wrong.
3. Add page directives to the front page of the ASPX page of the uploaded file. Errorpage= "Uploaderror.aspx"
4. Add some code in the error.aspx to determine if 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
{
Response.Redirect ("Uploadexcel.aspx?err=1"); Re-jump to upload page, plus err parameter to display error message
}
}

5, display error prompts.
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, finish the work. (This fish is not free.)

After the above changes, now the upload has become, the program specifies the file size, if more than, then pop-up error prompts.
In this way, much better, at least they can accept, and the user explanation is not too strenuous.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.