I had written couple of articles on uploading file user control, Bulk file uploader using C# and Asp.net 1.1 & ASP.net 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(" <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;up_div.style.color='red';}");
sb.Append("function StartShowWait(){up_div.style.visibility='visible';ShowWait();window.setInterval('ShowWait()',100);}");
sb.Append("StartShowWait();</script>");
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Flush();
}
//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!');");
sbc.Append("up_div.style.visibility='hidden';");
sbc.Append("history.go(-1)");
sbc.Append("</script>");
HttpContext.Current.Response.Write(sbc);
}
protected void btnUpload_Click(object sender, EventArgs e)
{
string strFileName = System.IO.Path.GetFileName(upUserCtrl.PostedFile.FileName);
try
{
if (strFileName != "")
{
//print progressbar
PrintProgressBar();
upUserCtrl.PostedFile.SaveAs(strLocation + strFileName);
Thread.Sleep(2000);
ClearProgressBar();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:Uploader ID="Uploader1" runat="server" />
</div>
</form>
</body>
</html>
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.

daniel fariasPosted Jun 4, 2013, 7:03 AM
if the file is too big like 5mb ou 9mb it does not work! ??
tin winPosted Mar 19, 2013, 5:10 AM
yes,this does not work in firefox
Nithin RajPosted Jun 7, 2012, 12:11 PM
jjj
Amit KumarPosted Apr 12, 2012, 3:52 PM
Thanks Munir! Awesome way to handle the inabilty of Ajax asynchronousity for fileUpload and Progress Bar. Kudos!
hitesh kalaPosted Dec 10, 2010, 7:27 AM
Hi, Thanks for such good article.. it's working fine in full page.. in pop up page,page gets hanged after uploading please suggest
Anil pandeyPosted Oct 18, 2010, 5:43 AM
I have checked this in IE and it run like a charm but on Mozilla the thing is gone, there is no image at all and the file in uploaded. What changes shall we do so that it can run on Mozilla also...
surya sasidharPosted Aug 17, 2010, 8:08 AM
Hi Munir, The code is working in IE but it is not working in Fire Fox The design issue also coming in fire fox the image is not displaying.....
surya sasidharPosted Aug 17, 2010, 7:33 AM
It is working fine
Pritam PratiharPosted Jun 8, 2009, 2:35 AM
U should mention that this works only with IE and hence eventually it is useless.
Scott TrickPosted Nov 6, 2008, 5:11 PM
Do you know of a way to access the filename in default.aspx?
Buddy BPosted Mar 5, 2008, 3:46 PM
Great job.. Code works great!! A+
bienwellPosted Oct 4, 2007, 8:19 AM
Hi all, I've tried the sample code above and saw the progressive bar while uploading file. The problem is when I selected the large file (around 120M bytes), I've got an error page with the message : "The page cannot be displayed". What's the problem ? Please advise. Thank you.
bienwellPosted Oct 4, 2007, 8:18 AM
Hi all, I've tried the sample code above and saw the progressive bar while uploading file. The problem is when I selected the large file (around 120M bytes), I've got an error page with the message : "The page cannot be displayed". What's the problem ? Please advise. Thank you.
Hanif MulaniPosted Sep 13, 2007, 6:21 AM
This is very simple code and it is very useful for Begineer. thanks, Hanif
Hanif MulaniPosted Sep 13, 2007, 6:21 AM
This is very simple code and it is very useful for Begineer. thanks, Hanif
jobin thomasPosted Aug 21, 2007, 5:25 AM
Thanks for your code sir,it is simple and can use is very easy.My problem is tht it doesn't give output when i run my page in mozilla. plz help me in this... thanks in advance...
davidPosted Aug 13, 2007, 5:35 PM
Hi, this works really well - thanks ! one question though as I am new to VS2005 - how do i mask the types of files the "open file" dialog can load ? I want to limit files to to extension "*.mp3" can u tell me how to do that ?
Tom HanseneditedPosted Aug 9, 2007, 5:58 PMEdited Aug 9, 2007, 6:05 PM
- sorry - it seems that I cannot post html tags in the comments. Munir if you want to see the code I can email it to you. - just did. - use it as you want.
Tom HansenPosted Aug 9, 2007, 5:56 PM
Played around a bit with this and added a page (Main.aspx) to host an iFrame to display your Default.aspx page. This way there is a bit more control over what goes on in the page and the HttpContext.Current.Response.Write. Also modified the Uploader.aspx.cs file a bit to display the uploaded filename in the usercontrol and in the Main.aspx page. ---- more to come
zeb qaumeditedPosted Aug 9, 2007, 1:12 PMEdited Aug 9, 2007, 2:54 PM
Its smart trick but how you can tie with saveAs command for example while uploading show progressbar instead of after saveAs finishes. Regards,ZEB
Divyesh ChapaneriPosted Aug 4, 2007, 9:04 AM
instead of javascript timer you could have gif image
Divyesh ChapaneriPosted Aug 4, 2007, 9:04 AM
instead of javascript timer you could have gif image
noe noePosted Jul 25, 2007, 5:00 AM
This article was great! The code is as simple as it can and it offers great results