Click here to Skip to main content
14,641,302 members
Articles » Web Development » Client side scripting » General
Technical Blog
Posted 17 Dec 2011

Tagged as


16 bookmarked

Use jQuery Uploader to Display File Upload Progress

Rate this:
5.00 (1 vote)
Please Sign up or sign in to vote.
5.00 (1 vote)
17 Dec 2011CPOL
This article explains the function and the use of Uploader

The original post can be found here.


Due to limited time, synchronization cannot be guaranteed in more than one blog article. At the following address, you can view up-to-date content, hope you understand:

Please download the sample code at the section jQueryElement demo download of Download jQueryElement, the directory is /uploader/Default.aspx.

This article explains the function and the use of Uploader; the catalog is as follows:

Image 1

Image 2

Image 3


Be sure that you have got the latest version of jQueryElement at the section JQueryElement.dll. Download JQueryElement.

Use the following statements to reference namespaces:

<%@ Register Assembly="zoyobar.shared.panzer.JQueryElement"


 TagPrefix="je" %>
<%@ Register Assembly="zoyobar.shared.panzer.JQueryElement"


 TagPrefix="je" %>

In addition to the namespace, you need to reference the jQueryUI scripts and styles. There is a custom theme for jQueryUI in the compressed file downloaded at the section JQueryElement.dll of Download jQueryElement. If you need more themes, you can get them at

<link type="text/css" rel="stylesheet"

 href="[style path]/jquery-ui-<version>.custom.css" />
<script type="text/javascript"

 src="[script path]/jquery-<version>.min.js"></script>
<script type="text/javascript"

 src="[script path]/jquery-ui-<version>.custom.min.js"></script>

Create Save Page

Save page is a simple page to save files; the save page does not submit itself, but is submitted by the upload page.

Add FileUpload Control

First, add a FileUpload control to the save page:

<form id="formFileUpload" runat="server">
Photo: <asp:FileUpload ID="file" runat="server" />

You can also add an input element whose attribute type is file:

<form id="formFileUpload" runat="server" enctype="multipart/form-data">
Photo: <input type="file" id="file" runat="server" />

If you use the input element, you may need to set the enctype attribute of the form to multipart/form-data.

Set EnableSessionState

The save page needs to set EnableSessionState to ReadOnly, so you can request the page that gets the progress when you save the file. This is mainly because ASP.NET orders pages that you can read and write to Session:

<%@ Page Language="C#" AutoEventWireup="true"

 CodeFile="FileUpload.aspx.cs" Inherits="uploader_FileUpload"

 EnableSessionState="ReadOnly" %>

Call the Save Method of Uploader

In the Page_Load method of the save page, call the static method Save of the Uploader control to save the file:

protected void Page_Load ( object sender, EventArgs e )

 if ( this.IsPostBack && this.file.HasFile )
  // TODO: Parameter waitSecond is set in order to show upload progress in test,
  // do not set waitSecond if you want to use this page,
  // and adjust the bufferSize to a reasonable value.
  Uploader.Save (
   this.Server.MapPath ( @"~/uploader/photo.jpg" ),
   this.Session["myphotouploadinfo"] as Uploader.UploadInfo,
   1 );
  // Uploader.Save (
  //  this.Server.MapPath ( @"~/uploader/photo.jpg" ),
  //  this.file.PostedFile,
  //  this.Session["myphotouploadinfo"] as Uploader.UploadInfo );


In the code, use the properties IsPostBack and HasFile to determine whether the user has submitted files.

The format of the method Save is Save ( string filePath, HttpPostedFile postedFile, Uploader.UploadInfo uploadInfo, int bufferSize, int waitSecond ). The parameter filePath is the full path of the saved file, postedFile is an HttpPostedFile object which can be obtained from the FileUpload control, uploadInfo is an object that contains the upload progress information, bufferSize is the cache size when you save the file, default is 128 KB, and waitSecond is used only during testing, which represents the wait time after the cache is saved, so you can ensure to see progress.

Create Page that Gets the Progress

The object Uploader.UploadInfo that contains the progress information is saved in the Session, so you can get progress information from the Session at any time:

<%@ WebHandler Language="C#" Class="uploader_getprec" %>

using System.Collections.Generic;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using zoyobar.shared.panzer.ui.jqueryui.plusin;

public class uploader_getprec : IHttpHandler,
 public void ProcessRequest ( HttpContext context )
  context.Response.ContentType = "text/javascript";
  context.Response.Cache.SetNoStore ( );

  Uploader.UploadInfo info =
   context.Session["myphotouploadinfo"] as Uploader.UploadInfo;

  SortedDictionary<string, object> json =
   new SortedDictionary<string, object> ( );

  if ( null == info )
   json.Add ( "prec", "-" );
   json.Add ( "prec", info.Percent );
   json.Add ( "total", info.ContentLength );
   json.Add ( "posted", info.PostedLength );

   if ( info.Percent == 100 )
    json.Add ( "url", "photo.jpg" );

    * {
    * "prec": 20.23
    * "total": 2000,
    * "posted": 2
    * }
    * */

  context.Response.Write (
   new JavaScriptSerializer ( ).Serialize ( json )



It is important to note, uploader_getprec implements the interface IReadOnlySessionState, rather than the IRequiresSessionState. The reason is similar to setting EnableSessionState to ReadOnly. As for how to return the JSON, please refer to Return JSON in Different .NET Versions.

Create Upload Page

The final step is to create the upload page, add the Uploader control in the page:

<je:Uploader ID="uploader" runat="server" IsVariable="true"
 ProgressUrl="getprec.ashx" ProgressChanged="

 if(-:data.prec == '-')
  $('#prec').text('no progress!');
  if(-:data.prec == 100){
   $('#prec').text('ok, the url is: ' + -:data.url);


   $('#photo').show().attr('src', -:data.url);
   $('#prec').text(-:data.posted +
    ' bytes/' + + ' bytes, ' +
    -:data.prec + '%');
   pb.progressbar('option', 'value', -:data.prec);
<je:Button ID="cmdUpload" runat="server" IsVariable="true"
 Label="Start" Click="

Set Save Page

By using the UploadUrl property of Uploader, you can choose the save page. In the example, we select FileUpload.aspx as the save page, which will automatically generate an iFrame element whose src attribute is FileUpload.aspx.

You can also customize an iframe, and then select this iFrame through the Upload property:

<iframe id="myIFrame" src="FileUpload.aspx"></iframe>

<je:Uploader ID="uploader" runat="server" IsVariable="true"


 ... >

Set Progress Page

Via the properties ProgressUrl and ProgressChanged, we can obtain and display the upload progress. ProgressUrl is the URL of the progress page, ProgressChanged will handle the progress information.

In addition, the ProgressInterval property represents the query interval of the progress page, the default is 1000 milliseconds.


Call the upload method of uploader to trigger the upload operation:

<je:Button ID="cmdUpload" runat="server" IsVariable="true"
 Label="Start" Click="

By default, the first form of the save page will be submitted; the index of form which will be submitted can be adjusted by the property UploadForm.

Hide Save Page

Call the hide method of uploader, you can hide the save page:


Related Content


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

Comments and Discussions

Questiongetprec.ashx Pin
Clingfree30-Dec-11 7:27
MemberClingfree30-Dec-11 7:27 
Why do you continue to call the getprec.ashx after the percentage is already 100%?
AnswerRe: getprec.ashx Pin
zoyobar30-Dec-11 21:59
Memberzoyobar30-Dec-11 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.