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; } } }
protected override void OnLoad(EventArgs e) { RegisterClientScriptBlock("NeatUpload-ProgressPage", "<script type='text/javascript' language='javascript' src='" + UploadModule.BustCache(ResolveUrl("Progress.js")) + @"'></script>"); SetupContext(); SetupBindableProps(); // Set the status to Cancelled if requested. if (Request.Params["cancelled"] == "true") { UploadModule.CancelPostBack(PostBackID); } if (Request.Params["useXml"] == "true") { Response.ContentType = "text/xml"; Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); Response.Write(@"<?xml version=""1.0"" encoding=""utf-8"" ?>"); } string prevStatus = Request.Params["prevStatus"]; if (prevStatus == null) { prevStatus = UploadStatus.Unknown.ToString(); } string curStatus = Status.ToString(); // If the status is unchanged from the last refresh and it is not Unknown nor *InProgress, // then the page is not refreshed. Instead, if an UploadException occurred we try to cancel // the upload. Otherwise, no exception occurred we close the progress display window (if it's a pop-up) if (curStatus == prevStatus && (Status != UploadStatus.Unknown && Status != UploadStatus.NormalInProgress && Status != UploadStatus.ChunkedInProgress && Status != UploadStatus.ProcessingInProgress )) { if (Status == UploadStatus.Rejected || (Status == UploadStatus.Failed && Failure is HttpException && ((HttpException)Failure).GetHttpCode() == 400)) { if (CanCancel) { RegisterStartupScript("scrNeatUploadError", @" <script type=""text/javascript"" language=""javascript""> <!-- window.onload = function() { NeatUploadStop('" + Status + @"'); } // --> </script>"); } } else if (Status != UploadStatus.Failed) { RegisterStartupScript("scrNeatUploadClose", @"<script type='text/javascript' language='javascript'> <!-- NeatUploadClose('" + ProgressBarID + @"'); // --> </script>"); } } // Otherwise, if we are refreshing, we refresh the page in one second else if (IsRefreshing) { // And add a prevStatus param to the url to track the previous status. string refreshUrl = RefreshUrl + "&prevStatus=" + curStatus; Refresh(refreshUrl); } base.OnLoad(e); }
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); }