ASP. NET Applications process file uploads

Source: Internet
Author: User
Tags ftp file

File Upload is a common requirement in Web programs. Using HTTP to upload files is very limited. The most common method is to use the <input type = "file"/> element to upload files. 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.

The biggest advantage of using <input type = "file"/> for file upload is convenient programming. Almost all server-side technologies have encapsulated this upload method, this allows programmers to process files uploaded on the client. 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 friends think that the biggest problem with using <input type = "file"/> file Upload is that the memory usage is too high, because the entire file needs to be loaded into the memory for processing, as a result, if the user files are uploaded too large or too many users are uploaded at the same time, the server memory will be exhausted. 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.

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 file upload cannot exceed 4 M, this is because the maxRequestLength size is 4096 by default, 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 file is uploaded 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.

If you need to process File Uploads in an ASP. NET application, you can directly use the <asp: FileUpload/> Control to upload files. If a page contains the <asp: FileUpload/> control, the enctype of the form element in the page is automatically changed to multipart/form-data, in addition, we can use the <asp: FileUpload/> control reference after the page PostBack to obtain the files uploaded by the client through the control. However, the <asp: FileUpload/> control is powerless if the file upload function requires a special requirement, for example, a progress bar prompt.

To be exact, the support provided by <input type = "file"/> is very limited, so we cannot implement some special requirements-strictly speaking, it should not be implemented easily or directly. In this way, when we implement these functions, we will go around a big bend. To avoid time-consuming detours every time you implement the same function, various Upload components have emerged. The Upload Component provides encapsulated functions, making it much easier to implement the file upload function. For example, almost all Upload components provide progress prompts directly or indirectly, some provide the current percentage value, and some directly provide a set of UI; some components only provide a simple UI, while others provide a complete set of upload and deletion management interfaces. In addition, some components provide the ability to prevent malicious client uploads.

About ASP. the most popular way to upload components under. NET is in ASP. NET Pipeline's BeginRequest event intercepts the current HttpWorkerRequest object, and then directly calls its ReadEntityBody and other methods to obtain the data stream passed by the client, and analyze and process it. In ASP. NET 1.1, the purpose of doing so is to directly write data to the hard disk, to avoid the consumption of too much server memory for the uploaded content, but now it is not because of this reason. It takes some time to initiate a request from the client to transmit data to a certain scale. It takes some time to read the data stream from the HttpWorkerRequest object, the client can use new requests for polling to obtain the current upload status. This is the most traditional way to get the upload progress. The principle of this practice is easy to understand, but it is not easy to write a complete component, especially the problems in various details. NeatUpload is the most successful and famous among these components.

NeatUpload is an open-source component that uses the LGPLLesser General Public License) License Agreement, that is, it is "business-friendly. NeatUpload can be used in ASP. NET and mono to store uploaded files on hard disks or in SQL Server databases. NeatUpload provides two server controls: <NeatUpload: InputFile> and <NeatUpload: ProgressBar>. The former is used to replace <asp: FileUpload/> and access the content uploaded by users in a specific upload box. The latter is a progress bar display control, displays the upload progress in a pop-up window or inline mode. The pop-up window does not need to be said. The so-called "inline" method only embeds an Iframe element in the page, then, you can refresh the page in iframe to display the progress. The difference between the display mode and the pop-up window is only in the position of the page. Of course, if we want to port it to the AJAX form, it is not difficult to develop a page, inherit the ProgressPage class provided by NeatUpload, and use the total number of bytes of some properties provided by ProgressPage, number of uploaded bytes, time spent, etc .) to get the progress of the current upload, and then directly use Response. write outputs data in JSON format. In fact, the pages originally in the iframe or new window also inherit the ProgressPage class and are rendered in HTML format. In essence, there is no big difference.

However, I personally think that the practical value of NeatUpload is not high. I will talk about it later.) The biggest significance of NeatUpload is to provide a complete and excellent example. NeatUpload is a rare Learning case with exquisite design and complete annotations. If you can study the NeatUpload code again, I believe that the programming capability and ASP. NET understanding will bring a new step. In addition, you can also find NeatHtml on the NeatUpload site. NeatHtml is an open-source Web component used to display insecure content. It is mainly used to prevent cross-site scripting (XSS, cross-Site Scripting) and other security issues. As the component author, Dean also summarized the technology used by NeatHtml into a Whitepaper. If you are interested, you can take a look at it. This is a rare piece of technical material.

Open-source components similar to NeatUpload include Memba Velodoc XP Edition, which is the core of the Velodoc file management system. Strictly speaking, this is not just an upload component, but a file management solution that includes:
1. an ASP. NET Http Module. It is interesting to support uploading large files. NeatUpload declares that a Bug in IIS 7 makes it unavailable in IIS 7 integrated pipeline mode ).
2. An ASP. NET Http Handler that supports resumable upload.
3. A series of ASP. NET Server-side controls provide the UI required for the file upload function, including a multi-File Upload control, a ListView control, and a progress bar control.
4. A Web application can replace the FTP file exchange mode and support sending links by Email. It is also an example of the component used above.
5. A Windows Service is used to regularly clear old files.
6. A test project, a deployment project, and an installation project.
7. Documentation.

Return to the NeatUpload component. To be honest, I never like this way of getting progress, because I think it is a burden to round the server with an extra request. In fact, if you need to upload a large file and get the upload progress, the best method is to use RIA. The most typical RIA upload method is Flash. The FileReference and FileReferenceList components already exist in ActionScript 2.0 to support single-file and multi-File Upload. With these two components, various information uploaded can be obtained from the client, the upload progress can also be calculated. The FileReference and FileReferenceList components are very easy to use. Even people like me who know nothing about Flash can make a simple upload function in a short time. However, with swfupload, the world has become better.

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:

 
 
  1. publicclassUploadHandler:IHttpHandler  
  2. {  
  3. publicvoidProcessRequest(HttpContextcontext)  
  4. {  
  5. HttpFileCollectionfileColllection=context.Request.Files;  
  6. ...  
  7. }  
  8.  
  9. publicboolIsReusable{...}  

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 file upload in ASP. NET. Is there anything I haven't covered? I personally think there is at least one very important issue that has not been discussed, that is, the issue that ASP. NET Processing threads are occupied when ASP. NET Applications process file uploads. 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 upload connection, 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 file upload or processing process 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 upload thread.

  1. Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
  2. ASP. NET Server custom control security guidelines
  3. Analysis of ASP. NET programming standards and Their encoding specifications
  4. Introduction to ASP. NET sessions
  5. ASP. NET programming tool ASP. NET Web Matrix

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.