private void Control_PreRender(object sender, EventArgs args) { if (IsDesignTime) { return; } // Find the current upload context string postBackID = Page.Request.Params["postBackID"]; string barID = Page.Request.Params["barID"]; // Set the status to Cancelled if requested. if (Page.Request.Params["cancelled"] == "true") { UploadModule.CancelPostBack(postBackID); } IUploadProgressState progressState = new UploadProgressState(); UploadModule.BindProgressState(postBackID, barID, progressState); if (progressState.Status == UploadStatus.Unknown) { // Status is unknown, so try to find the last post back based on the lastPostBackID param. // If successful, use the status of the last post back. string lastPostBackID = Page.Request.Params["lastPostBackID"]; if (lastPostBackID != null && lastPostBackID.Length > 0 && Page.Request.Params["refresher"] == null) { UploadModule.BindProgressState(lastPostBackID, barID, progressState); if (progressState.FileBytesRead == 0 && progressState.ProcessingState == null) { progressState.Status = UploadStatus.Unknown; } } } Status = progressState.Status; ProgressInfo progress = null; progress = (ProgressInfo)progressState.ProcessingState; if (progress != null && Status == UploadStatus.Completed) { Status = UploadStatus.ProcessingCompleted; } Attributes["status"] = Status.ToString(); if (WhenStatus != null) { string[] matchingStatuses = WhenStatus.Split(' '); if (Array.IndexOf(matchingStatuses, Status.ToString()) == -1) { this.Visible = false; } } }
private void SetupContext() { // Find the current upload context PostBackID = Request.Params["postBackID"]; ProgressBarID = Request.Params["barID"]; UploadModule.BindProgressState(PostBackID, ProgressBarID, this); if (log.IsDebugEnabled) { log.Debug("Status " + Status + " when SessionID = " + (HttpContext.Current.Session != null ? HttpContext.Current.Session.SessionID : null)); } if (Status == UploadStatus.Unknown) { CurrentStatus = UploadStatus.Unknown; // Status is unknown, so try to find the last post back based on the lastPostBackID param. // If successful, use the status of the last post back. string lastPostBackID = Page.Request.Params["lastPostBackID"]; if (lastPostBackID != null && lastPostBackID.Length > 0 && Page.Request.Params["refresher"] == null) { UploadModule.BindProgressState(lastPostBackID, ProgressBarID, this); if (FileBytesRead == 0 && ProcessingState != null) { Status = UploadStatus.Unknown; } } } else { if (log.IsDebugEnabled) { log.Debug("In ProgressPage, PostBackID = " + PostBackID); } if (log.IsDebugEnabled) { log.Debug("In ProgressPage, Status = " + Status); } CurrentStatus = Status; } }
public void ProcessRequest(HttpContext context) { string postBackID = context.Request.Params["postBackID"]; string controlID = context.Request.Params["controlID"]; string cancel = context.Request.Params["cancel"]; if (cancel != null && Boolean.Parse(cancel)) { UploadModule.CancelPostBack(postBackID); } UploadModule.BindProgressState(postBackID, controlID, this); string message = ""; if (Status == UploadStatus.Rejected) { message = Rejection.Message; } else if (Status == UploadStatus.Failed) { message = Failure.Message; } context.Response.ContentType = "application/json"; double percentComplete = Math.Floor(100 * FractionComplete); string processingStateJson = "null"; ProgressInfo progressInfo = ProcessingState as ProgressInfo; if (progressInfo != null) { percentComplete = Math.Floor(100.0 * progressInfo.Value / progressInfo.Maximum); processingStateJson = String.Format(@"{{ ""Value"" : {0}, ""Maximum"" : {1}, ""Units"" : ""{2}"", ""Text"" : ""{3}"" }}", progressInfo.Value, progressInfo.Maximum, Quote(progressInfo.Units), progressInfo.Text != null ? Quote(progressInfo.Text) : ""); } System.Text.StringBuilder filesJson = new System.Text.StringBuilder(); bool isFirstFile = true; for (int i = 0; Files != null && i < Files.Count; i++) { UploadedFile file = Files[i]; if (Status == UploadStatus.NormalInProgress || Status == UploadStatus.ChunkedInProgress || Status == UploadStatus.ProcessingInProgress) { AppendFileJson(filesJson, file, ref isFirstFile); } else { // If the upload isn't in progress, the file might have been disposed which // can cause exceptions, so we only make a best effort. try { AppendFileJson(filesJson, file, ref isFirstFile); } catch (Exception ex) { // File was probably disposed so ignore the error. } } } string jsonFormat = @"{{ ""Status"" : ""{0}"", ""BytesRead"" : {1}, ""BytesTotal"" : {2}, ""PercentComplete"" : {3}, ""BytesPerSec"" : {4}, ""Message"" : ""{5}"", ""SecsRemaining"" : {6}, ""SecsElapsed"" : {7}, ""CurrentFileName"" : ""{8}"", ""ProcessingState"" : {9}, ""Files"" : [{10}] }} "; string json = String.Format(jsonFormat, Status, BytesRead, BytesTotal, percentComplete, BytesPerSec, Quote(message), Math.Floor(TimeRemaining.TotalSeconds), Math.Floor(TimeElapsed.TotalSeconds), Quote(CurrentFileName), processingStateJson, filesJson.ToString()); context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); context.Response.Write(json); }