Introduction to ASP. NET Applications

Source: Internet
Author: User

Uploading files in Web programs is a common requirement. Using HTTP to upload files is very limited. The most common method is to useElement. This upload method uses the multipart/form-data scheme to encode the content and POST the content to the server. The multipart/form-data encoding method is much more efficient than the default application/x-url-encoded encoding method.

UseThe biggest advantage of file uploading is convenient programming. Almost all server-side technologies have encapsulated this upload method, allowing programmers to process files uploaded on the client intuitively. However, in general, this protocol is not suitable for file transmission, and the cost of parsing data streams is relatively high, and there is no such mechanism as resumable data transfer, as a result, uploading large files often fails.

Some people think thatThe biggest problem with uploading files is that the memory usage is too high. Because the entire file needs to be loaded into the memory for processing, if the user uploads too many files or uploads too many users at the same time, this will cause the server to run out of memory. This is actually wrong. Some server-side technologies, such as Spring Framework, or early ASP. NET 1.1, fully load user-uploaded content into the memory for processing by the program, which indeed brings problems. However, the Protocol does not specify the method used by the server to process uploaded files. For example, in the current ASP. NET 2.0 already exists in a temporary file on the hard disk after the user uploads more data. This is completely transparent to developers, that is, developers can process data streams as before.


Swfupload is also an open-source component. As its name suggests, it uses Flash for upload. However, for swfupload, Flash is mainly used for "control" rather than "display", which undoubtedly gives developers more flexibility. The swfupload implementation method naturally utilizes the functions provided by the FileReference and FileReferenceList components. Through the interaction between Flash and JavaScript, the development file upload function becomes very elegant and easy. With swfupload, developers can use JavaScript to implement various display methods. It is no longer very difficult to develop a cool upload interface like Flicker.

Swfupload is a client component that is completely transparent to the server. That is to say, the server only needs to process the common form. For example, in ASP. NET, we can use Generic Handler to process client file uploads. As shown in the following figure, the fileCollection variable is the set of all files from the client Post to the server. You can obtain the HttpPostedFile object using the name or the following method.

ASP. NET 2.0 enable the threshold value threshold for the temporary files on the hard disk) is configurable:

 
 
  1. <system.web> 
  2. <httpRuntime 
  3. maxRequestLength="Int32" 
  4. requestLengthDiskThreshold="Int32" /> 
  5. system.web> 

MaxRequestLength does not have to be said, just got in touch with ASP. NET friends will always find that the size of the uploaded file cannot exceed 4 MB, which is because the default maxRequestLength is 4096, which limits the size of each request to not exceed 40 96kb. This is intended to protect applications from malicious requests. When the request exceeds maxRequestLength, the ASP. NET handler will not process the request. Here and ASP. NET throws an exception, which is why if the user uploads a file too large, it is not ASP. the error page specified in the. NET application or the default one), because ASP. NET has not processed this request. RequestLengthDiskThreshold is the threshold value just mentioned. Its default value is 256. That is, when the request content exceeds kb, the hard disk is used as the cache. This threshold is theoretically irrelevant to whether the client is uploading content, as long as the request sent by the client is greater than this threshold. Therefore, in ASP. NET 2.0, the server's memory will not be exhausted due to abnormal client requests.

Since Flash provides the file upload function, Silverlight, as the main tool of Microsoft, does not lack this function. This article is derived from the Quick Starts of Silverlight 2.0 and shows how to use the file upload function developed by Silverlight 2.0. If you are interested, you can read it.

I have discussed a lot about uploading files in ASP. NET. Is there anything that is not involved? I personally think there is at least one very important issue that has not been discussed, that is, the issue of occupying ASP. NET processing threads when processing uploaded files. As we all know, ASP. NET will use threads in the thread pool to process requests. When threads in the thread pool are used up, requests that are not processed can only be queued. Therefore, an important way to increase the throughput of ASP. NET applications is to use Asynchronous processing methods for some time-consuming operations. In fact, this proposition can be applied in most applications ). For example, it takes three seconds to query a database. If asynchronous operations are not used, the processing thread will be blocked until the query is completed. If you use the Asynchronous Method to execute database queries, the thread can process other requests within these three seconds. After the asynchronous operation is completed, ASP. NET will use another thread to continue processing this request.

Uploading large files is also a task that takes up processing threads for a long time. Unfortunately, asynchronous operations cannot be used to release processing threads. This requires the support of the operating system, therefore, only a small number of functions can use asynchronous operations ). If it takes three minutes to upload a file, a processing thread is exclusive within these three minutes. If there are more than one file to be uploaded, it will greatly affect the performance of the application-just like suffering some DOS attack. Therefore, even if components such as NeatUpload and swfupload are used, the problem of excessive upload connections and the reduction of available threads cannot be solved. It is not easy to solve this problem. You are welcome to discuss the problem in the following two ways ):

◆ Expand IIS so that the process of uploading or processing files is not processed by ASP. NET, so as to reduce the consumption of ASP. NET application threads. Now with IIS 7, if you use the Integrated pipeline mode, you can also use managed code for extension.
◆ Use an additional ASP. NET application to process file uploads to save the consumption of the original ASP. NET application thread by the file uploading thread.

  1. ASP. NET TypeConverter
  2. Analysis on TypeResolver of ASP. NET
  3. Define JavaScriptConverter in ASP. NET
  4. How to replace Sys. Services in ASP. NET
  5. Use Profile Service of ASP. NET AJAX

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.