ASP. NET MVC 3 Implementation progress bar upload ideas [go]

Source: Internet
Author: User

Recently do ASP. NET MVC file upload, but each time the large file upload response time is very long, no hint can not give the user a good experience, so I think it is necessary to display the progress bar in the file upload, but the default file upload pipeline is not able to display the progress bar. Therefore, you must manually create a receive pipeline yourself. There are 2 ways to implement, one is to write a class to inherit the IHttpModule interface and then implement the method, there is a direct global.asax inside the Application_BeginRequest to write the method of uploading files.

When writing a File receive method should be chunked to receive files, and then save the state of the file at the same time, it is important to note how to save the state of the file, so that the client gets, I think a few ways to summarize

1. You can use the application variable. The Application object is scoped to the whole global, which means it is valid for all users. Its common method is to use lock and unlock.

?
12345 Application.Lock(); Application["GUID"]=upload;Application.UnLock();

2, use the session variable. Presumably this is the most common usage of everyone, its operation is similar to application, acting on the user's personal, so, excessive storage will lead to the depletion of server memory resources.

?
1 Session["GUID"]=upload;

  

3. Use cookie variables. This is also common use of the method, as in the session, in fact, for each user, but they have an essential difference, that is, the cookie is stored in the client, and the session is stored on the server side. And the use of cookies is to be invoked with the ASP. NET built-in object request.

?
12345 HttpCookie myCookie = newHttpCookie(cookieName);myCookie = HttpContext.Current.Request.Cookies[cookieName];HttpContext.Current.Response.Cookies.Add(myCookie);HttpCookie myCookie = newHttpCookie(cookieName);myCookie = HttpContext.Current.Request.Cookies[cookieName];

  

4, use the cache, but. NET caches have two ways of calling: 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: Gets the Cache for the current application. HttpContext: Encapsulates all HTTP-specific information about an individual HTTP request,HttpContext.Current Gets the HttpContext object for the current HTTP request. HttpRuntime: Provides a set of ASP. NET Runtime services for the current application.

In fact, Httpruntime.cache equivalent is a cache specific implementation class, although this class is placed in the System.Web namespace, but not under the Web application is also available, HttpContext.Current.Cache is the encapsulation of the above-mentioned cache class, as encapsulated in the HTTP In the context class, it is limited to use only under the knowledge of HttpContext, that is, only for Web applications.

Since we are using MVC3 as a Web application, we can all use it, but I would suggest that it is a Web application or a HttpContext.Current.Cache.

?
1 HttpContext.Current.Cache[Guid]

--------------------------------------------------------------------------------------------------------------- ----------------------------

The above 4 kinds can be considered as global variables, but some are not very good solutions, we recommend using the first and fourth to store data. I use the cache to store information about the progress of a file.

Now there is another problem. How do we know what application is currently being uploaded? I use a GUID to identify, so that the uploaded program has a unique identifier. There is no problem with multiple users uploading at the same time.

On the client side I used the jquery version number 1.64, jquery.blockUI.js version number 1.23, jquery.form.js version number 2.84, jquery-ui.js version number 1.8.16

Using Jquery.blockui to implement the popup layer, Jquery.form used to submit data, jquery $.ajax method to get the current file progress status Json,jquery-ui to implement the progress bar information.

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.