Click here to Skip to main content
15,794,629 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:

In my web application, i need to show 'Progress Bar' of uploading status. I got the source from one of the CP member's post.Link

Problem is,
I'm using master page and Content page for my whole application. While i try to upload from an simple aspx page it works fine. But while i tried using Master and Content its not works well.

My code structure is,

One of my content page contains <Frame> for loading a simple aspx page
<iframe id="uploadFrame" frameborder="0" scrolling="yes" src="UploadEngine.aspx">  </iframe>

where "UploadEngine.aspx" Contains Code to Save a Particular File on Server.

Here we have an Class file named "UploadDetail.cs" contains four different variables used to maintain in Session for obtaining current upload status from "UploadEngine.aspx"

Hence while i click upload button from a Content page which contains <Iframe>. It just calls the "UploadEngine.aspx" and starts upload the files into my server's specified folder, but i can't getting the session variables updated for showing progress status. Its always remains null.

Hope you understand !
Please help me out...

My Upload.aspx

//Start upload process
function onUploadClick() {
if (fileUpload.value.length > 0) {
    if (true) {
        //Update the message, for respective Div through different functions 
        updateMessage(MessageStatus.Information, 'Initializing upload ...', '', '0 of 0 Bytes');
        //Submit the form containing the fileupload control
        form.submit(); //here i'm submitting "UploadEngine.aspx.cs" to start Upload
        Sys.UI.DomElement.addCssClass($get('dvUploader'), 'StartUpload');
        //Initialize progressbar
        intervalID = window.setInterval(function() {
        PageMethods.GetUploadStatus(function(result) {
            if (result) {
                //Upadte the message every 500 milisecond
                updateMessage(MessageStatus.Information, result.message, result.fileName, result.downloadBytes);
                if (result == 100) {
                    //clear the interval
        }, 500);
        onComplete(MessageStatus.Error, "File name '" + filename + "' already exists in the list.", '', '0 of 0 Bytes');
    onComplete(MessageStatus.Warning, 'You need to select a file.', '', '0 of 0 Bytes');

My Upload.aspx.cs

protected void Page_Load(object sender, EventArgs e)
       if (!this.IsPostBack)
           //Reserve a spot in Session for the UploadDetail object
           this.Session["UploadDetail"] = new UploadDetail { IsReady = false };


  public static object GetUploadStatus()
      //Get the length of the file on disk and divide that by the length of the stream
      UploadDetail info = (UploadDetail)HttpContext.Current.Session["UploadDetail"];
      if (info != null && info.IsReady)
          long soFar = info.UploadedLength;
          long total = info.ContentLength;
          int percentComplete = (int)Math.Ceiling((double)soFar / (double)total * 100);
          string message = "Uploading...";
          string fileName = string.Format("{0}", info.FileName);
          string downloadBytes = string.Format("{0} of {1} Bytes", soFar, total);
          return new
              percentComplete = percentComplete,
              message = message,
              fileName = fileName,
              downloadBytes = downloadBytes
      //Not ready yet
      return null;

public class UploadDetail
    public bool IsReady { get; set; }
    public long  ContentLength { get; set; }
    public long  UploadedLength { get; set; }
    public long  TotalUploadedLength { get; set; }
    public string FileName { get; set; }


protected void Page_Load(object sender, EventArgs e)
        if (this.IsPostBack)
            UploadDetail Upload = (UploadDetail)this.Session["UploadDetail"];
            //Let the webservie know that we are not yet ready
            Upload.IsReady = false;

            HttpFileCollection hfc = Request.Files;

            string fileName = "";
            int ContentTotalLen = 0;
            for (int k = 0; k < hfc.Count; k++)
                HttpPostedFile hhpf = hfc[k];
                ContentTotalLen += hhpf.ContentLength;

            if (ContentTotalLen > 0)
                //build the local path where upload all the files
                string path = @"E:\FTPFILES\";

                //Let the polling process know that we are done initializing ...
                Upload.IsReady = true;
                Upload.TotalUploadedLength = 0;

                for (int i = 0; i < hfc.Count; i++)
                    HttpPostedFile hpf = hfc[i];
                    fileName = System.IO.Path.GetFileName(hpf.FileName);

                    //Build the strucutre and stuff it into session
                    Upload.ContentLength = hfc[i].ContentLength;

                    Upload.UploadedLength = 0;

                    if (fileName != "")
                        Upload.FileName = fileName;

                        //set the buffer size to something larger.
                        //the smaller the buffer the longer it will take to download, 
                        //but the more precise your progress bar will be.
                        int bufferSize = 1;
                        byte[] buffer = new byte[bufferSize];

                        //Writing the byte to disk
                        using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.Create))
                            //As long was we haven't written everything...
                            while (Upload.UploadedLength < Upload.ContentLength)

                                //Fill the buffer from the input stream
                                int bytes = hfc[i].InputStream.Read(buffer, 0, bufferSize);
                                //Writing the bytes to the file stream
                                fs.Write(buffer, 0, bytes);
                                //Update the number the webservice is polling on to the session
                                Upload.UploadedLength += bytes;

                                //for Complete function
                                Upload.TotalUploadedLength += bytes;



                //Call parent page know we have processed the uplaod
                const string js = "window.parent.onComplete(1,'File uploaded successfully.','{0}','{1} of {2} Bytes');";
                ScriptManager.RegisterStartupScript(this, typeof(UploadEngine), "progress", string.Format(js, fileName, Upload.TotalUploadedLength, ContentTotalLen), true);

                //Call parent page know we have processed the uplaod
                const string js = "window.parent.onComplete(4, 'There was a problem with the file.','','0 of 0 Bytes');";
                ScriptManager.RegisterStartupScript(this, typeof(UploadEngine), "progress", js, true);

Updated 2-Apr-12 6:46am
Shahin Khorshidnia 31-Mar-12 7:45am    
Let's see the Session initializing code.
J.Karthick 31-Mar-12 8:46am    
Thanks for your comment...,
Please check the code above.
rohit_singhk 8-Apr-13 7:06am    
I am also facing problem dear can you please solve.
When i upload same code on Server then upload process bar not working else local working fine .

Please solve it.

1 solution

I have found the problem is with the "SessionState" i'm using.(After a struggle of two days)

Actually i'm using SQLSERVER session. hence my page not goes back and get the session values appropriately(i guess)

Now while i changing the session state as "STATESERVER" in IIS, its works fine and updating the uploaded contents well.

I have posted the above code for the future reference.
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900