Analysis of ASP. NET large file upload Methods

Source: Internet
Author: User

Developers who have used ASP. Net know that the fileupload control is a double-edged sword-it may be our savior and an enemy. One of the most common problems is how to upload large files larger than 4 MB.

Microsoft's ASP. net mvp Jon Galloway recently published an articleArticleAnd discussed various usage techniques of the fileupload control. After learning about this, we can theoretically process file uploads of any size.

Allow ASP. NET large file uploads

Jon mentioned that using the fileupload control to upload files is very tricky. Developers should understand that the default file size limit is 4 MB, not because the design staff came to light, but to avoid potential DoS attacks.

If an attacker submits one or more large files, the server is often overwhelmed. If the size of the File Uploaded by the user is greater than 4 MB, an error message "Maximum request length exceeded." is displayed.

It is not difficult to increase the size limit, but developers need to know how to do it is the best method. The default 4 MB setting is in the system's machine. config file, but we can overwrite this value in Web. config.

For example, if you want to increase the Upload File ceiling to 20 mb, you only need to modify it as follows:

 
 
  1. <System. Web> < System. Web > < Httpruntime executiontimeout = " 240 " Maxrequestlength = " 20480 "   /> </ System. Web >

If the value is modified in machine. config, it is not only the website that is affected at the same time. ASP. NET is designed to avoid potential attacks. So the best way is to overwrite in a specific directory, instead of the entire application.Program. The format of the web. config file is very flexible, so it is not difficult to achieve this:

The Web. config file allows cascading coverage, so this requirement can be easily implemented. We can add web. config file, and write the above configuration, or simply in the web. add a special <location/> label to the config file to achieve the same effect:

 
 
  1. <Location Path = "Upload"> <system. Web> </System. Web> </location> < Location Path = " Upload " >
    < System. Web >
    < Httpruntime executiontimeout = " 110 " Maxrequestlength = " 20000 "   />  
    </ System. Web >
    </ Location >

To allow ASP. NET large file uploads, changing the default file size limit is only the first step.

If ASP. NET is used to upload large files, some interesting situations may occur. No matter what maxrequestlength is set in, IIS will accept it without thinking about it, but an exception will be thrown during ASP. NET check.

Of course, this exception can be easily captured, but this is not what we expect. Another method is to override the page. onerror method and check the HTTP response when an httpexception occurs.CodeWhether it is 400, but this is not perfect.

Prompt users

One way to dislike users is to mislead users and give them no prompt when performing operations on Web applications. The limitation of file size is written in Web. config, so it is very natural to put a text prompt in Web. config.

The best practice is to read the httpruntime section in Web. config at runtime and convert it to the httpruntimesection object.

Better solutions

There are also some commercial solutions for us to choose from. These solutions are implemented through httphandler. While giving users a full prompt through the progress bar, our developers can better control the file size and possible exceptions during the upload process.

Below are some common ASP. NET large File Upload components:

◆ Fileuploader. Net (mediachase, $310 or above)

◆ Radupload (telerik, $249)

◆ Neatupload (free of charge, compliant with the lgpl Protocol)

The article suggests that the best solution is to use Ria, such as the Upload components written in ASP. NET and Silverlight.

In most cases, we recommend that you use the Upload Component for Silverlight or flash to replace the traditional fileupload component. This type of component not only provides a better upload experience, but also looks better than the text box and button generated on the <input type = "file"> page. This <input type = "file"> does not support adding styles through CSS, although some people always try to find some methods.

Although there are currently no commercial Upload components using Silverlight, we can find a sample program to demonstrate how to use Silverlight to upload multiple files.

Even if it is a trivial problem like file upload, We can find many different ways to solve it. The biggest challenge lies in analyzing the advantages and disadvantages of different practices and measuring the time and money it takes to solve the problem with each practice. Only after careful analysis of the plan can we find the most suitable method for use in the project.

[Reprint] [http://developer.51cto.com/art/200907/137927.htm]

Related Article

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.