Upload files in ASP. NET applications.

Source: Internet
Author: User
Tags ftp file silverlight
Uploading files in web programs is a common requirement. 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"/> to upload files 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"/> to upload files 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 you upload too many files or upload too many users 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.

The threshold value (threshold) for enabling temporary files on the hard disk in ASP. NET 2.0 is configurable:

<System. Web>
<Httpruntime
Maxrequestlength = "int32"
Requestlengthdiskthreshold = "int32"/>
</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.

If we need. net (if not specified, the following ASP. net indicates ASP.. NET 2.0) When uploading files in an application, we usually 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 lgpl (lesser General Public License) license protocol, 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 pass some properties provided by progresspage (total number of bytes, 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 IFRAME (or new window) 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 (this will be described 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 (mainly user input content, such as blog comments and Forum posts). It is mainly used to avoid 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.

By the way, I personally think that many developers do not have sufficient programming skills. It seems that many people have put their focus on "design" or a specific technology too early, while ignoring the most basic "programming capabilities", that is, the ability to transform a piece of thinking into code implementation. I found that many of my friends seem to be able to quickly get a solution and describe it when solving the problem, but it is difficult to use the code to demonstrate it. In fact, in work, ideas or solutions can be obtained through discussion, but you can only rely on yourself when actually converting to code. In addition, the programming ability is not related to the so-called "work experience". I suggest you exercise your programming ability with your friends who are "fresh graduates" and "self-built.

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 that supports uploading large files (interestingly, 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 method and support sending links by email. It is also an example of the component used above.
  5. A Windows service that regularly cleans old files.
  6. One test project, one deployment project, and one 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.

Strictly speaking, the upload progress obtained through filereference is the "client data sending progress", while the neatupload method gets the "server data receiving progress", which cannot be confused.

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. :

Public class uploadhandler: ihttphandler
{
Public void processrequest (httpcontext context)
{
Httpfilecollection filecolllection = context. Request. files;
...
}
 
Public bool isreusable {...}
}

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 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, this cannot be done using asynchronous operations (releasing processing threads through asynchronous operations 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. Here are two ideas (You are welcome to discuss this ):

  • 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 upload thread.

Let's talk about it first.

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.