Progress Bar in ASP.NET 2.0 using File uploader user control

I had written couple of articles on uploading file user control, Bulk file uploader using C# and 1.1 & 2.0

Many users have written to me that if I ever thought of writing a progress bar while uploading file, so I just find a way as how we can write a progress bar, may be you can extend it as per your idea later.

To give you a detail in my sample application I have "Uploader.ascx" user control which hold Fileupload control and upload submit button. The code is as below:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Uploader.ascx.cs" Inherits="Uploader" %>

<asp:FileUpload ID="upUserCtrl" runat="server" />

<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />

"Uploader.ascx.cs" which is code behind page in which I have written separate function to print & once the upload finish clear the progress bar, both the functions are JavaScript functions, which are written at the server side, here I have done a trick to have a sort of progress bar, I have function ShowWait() javascript function which prints progressbar in "<div>" tag

Entire functionality as below:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Text;

using System.Threading;

public partial class Uploader : System.Web.UI.UserControl


    public string strLocation = "c:\\inetpub\\wwwroot\\ProgressBar\\ToUpload\\";

    protected void Page_Load(object sender, EventArgs e)



    //Javascript function to print progressbar

    public static void PrintProgressBar()


        StringBuilder sb = new StringBuilder();

        sb.Append("<div id='updiv' style='Font-weight:bold;font-size:11pt;Left:320px;COLOR:black;font-family:verdana;Position:absolute;Top:140px;Text-Align:center;'>");

        sb.Append("&nbsp;<script> var up_div=document.getElementById('updiv');up_div.innerText='';</script>");

        sb.Append("<script language=javascript>");

        sb.Append("var dts=0; var dtmax=10;");

        sb.Append("function ShowWait(){var output;output='Please wait while uploading!';dts++;if(dts>=dtmax)dts=1;");

        sb.Append("for(var x=0;x < dts; x++){output+='';}up_div.innerText=output;'red';}");

        sb.Append("function StartShowWait(){'visible';ShowWait();window.setInterval('ShowWait()',100);}");





    //Javascript function to clear progressbar

    public static void ClearProgressBar()


        StringBuilder sbc = new StringBuilder();

        sbc.Append("<script language='javascript'>");

        sbc.Append("alert('Upload process completed successfully!');");






    protected void btnUpload_Click(object sender, EventArgs e)


        string strFileName = System.IO.Path.GetFileName(upUserCtrl.PostedFile.FileName);



            if (strFileName != "")


                //print progressbar


                upUserCtrl.PostedFile.SaveAs(strLocation + strFileName);





        catch (Exception ex)






Note: "ToUpload" is a folder where I am uploading the files and by the time you must be aware that to upload file in any of the server side folder you should have permission i.e. "ASPNET" user should have full permission on the folder so that you can upload file.

I have "Default.aspx" page on which I had called the user control for uploading file, which is as below:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Src="Uploader.ascx" TagName="Uploader" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >

<head id="Head1" runat="server">

    <title>Untitled Page</title>



    <form id="form1" runat="server">


        <uc1:Uploader ID="Uploader1" runat="server" />





I have attached image here which will give you clear idea as how this functionality works. May be some of you can put this progress bar functionality in Pop up window also.