I recently uploaded a large asp.net mvc file. However, every time a large file is uploaded, the response time is very long. Without a prompt, the user experience will not be good, the progress bar must be displayed during file upload. However, the default file upload pipeline in asp.net cannot display the progress bar. Therefore, you must manually create a receiving pipeline. There are two implementation methods: one is to write a class that inherits the IHttpModule interface and then implement the method, and the other is to write the upload file directly in Application_BeginRequest in Global. asax.
When writing a file receiving method, you should block it to receive the file, and then save the file status at the same time. At this time, you must pay attention to how to save the file status so that the client can obtain it, I thought about several methods to summarize them.
1. You can use the Application variable. The scope of the Application object is global, that is, it is valid for all users. The common methods are Lock and UnLock.
View sourceprint? 1 Application. Lock ();
2
3 Application ["GUID"] = upload;
4
5 Application. UnLock ();
2. Use Session variables. Presumably, this is definitely the most common usage. Its operations are similar to those of the Application, which act on individual users. Therefore, excessive storage will result in the depletion of server memory resources.
View sourceprint? 1 Session ["GUID"] = upload;
3. Use Cookie variables. This is also a common method. Like Session, it is actually for every user, but they have a fundamental difference, that is, cookies are stored on the client, session is stored on the server. In addition, the use of cookies should be called with the ASP. NET built-in Object Request.
View sourceprint? 1 HttpCookie myCookie = new HttpCookie (cookieName );
2 myCookie = HttpContext. Current. Request. Cookies [cookieName];
3 HttpContext. Current. Response. Cookies. Add (myCookie );
4 HttpCookie myCookie = new HttpCookie (cookieName );
5 myCookie = HttpContext. Current. Request. Cookies [cookieName];
4. Use the Cache, but the Cache in. NET has two calling methods: HttpContext. Current. Cache and HttpRuntime. Cache. The following is an explanation of MSDN.
HttpContext. Current. Cache: gets the Cache object for the Current HTTP request.
HttpRuntime. Cache: Get the Cache of the current application.
HttpContext: encapsulate all HTTP-specific information about individual HTTP requests. HttpContext. Current is the Current HTTP request to obtain the HttpContext object.
HttpRuntime: provides a set of ASP. NET runtime services for the current application.
In fact, HttpRuntime. the Cache is equivalent to a specific implementation class of the Cache, although this class is placed in the System. httpContext. current. cache is the encapsulation of the preceding Cache class. Because it is encapsulated into the HttpContext class, it can only be used in the HttpContext class, that is, it can only be used for Web applications.
Since mvc3 is used as a web application, we can use it all, but I suggest using HttpContext. Current. Cache.
View sourceprint? 1 HttpContext. Current. Cache [Guid]
Certificate -------------------------------------------------------------------------------------------------------------------------------------------
The above four types can be regarded as global variables, but some are not a good solution. We recommend that you use the first and fourth types to store data. I use Cache to store file progress information.
At this time there is another problem. How do we know what application is being uploaded? I use GUID to identify the uploaded program. Multiple users can upload files at the same time.
On the client side I used Jquery version 1.64, jquery. blockUI. js version 1.23, jquery. form. js version 2.84, jquery-ui.js version 1.8.16
Jquery. blockUI is used to implement the pop-up layer. jquery. form is used to submit data. Jquery's $. ajax method is used to obtain the status Json of the current file progress, and jquery-ui is used to implement the progress bar information.
The following describes how to upload a large MVC3 file. This article mainly discusses the idea of implementing a progress bar for file upload and does not provide a demo for downloading. If you have any questions, leave a message.
Author: "Soy milk coffee"