Search code examples
c#httphandler

Reload user control after download file


In one of forms in my webform project, there is a user control. this is what I want to do When user click on "download" button in this user control:

  1. download file
  2. change a related record in db (for change user status)
  3. reload that user control (or whole form) to show information based on new status.

I try this solution but the user control doesn't reload.This is body of onclick event :

  Response.Redirect("~/Handlers/DownloadFile.ashx?FilePath=CourseFiles/" + _secondFilePath, false);
                _secondFilePath = string.Empty;
                CourseDB.ChangeCourseStep(DB.CurrentUser.ID, _lastUnpassedCourseInfo.CourseID, (int)Data.Enums.CourseStep.SecondPartFilesDownloaded);
                Response.AddHeader("Refresh", "3; url=~/Profile/SalesEngineeringCourse.aspx");
                Response.End();

and this is body of HttpHandler :

 public void ProcessRequest(HttpContext context)
    {
        string filePath = HttpContext.Current.Request.QueryString["FilePath"];
        string fileName = filePath.Split('\\').Last();
        string fileType = filePath.Split('.').Last();
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType = fileType;
        response.AddHeader("Content-Disposition",
                           "attachment; filename = " + fileName);
        response.TransmitFile(System.Web.HttpContext.Current.Server.MapPath("~") + filePath);
        response.Flush();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

is there any solution for this?


Solution

  • It's not possible to refresh the page after the download has started, because the server has already sent the header when the download starts, your only option is to use some javascript What I would suggest is the following: 1 - Change a related record in db (for change user status) 2 - Download file in a new windows by using javascript (see ScriptManager.RegisterStartupScript to achieve that) 3 – Do the refresh of the data in your page to show the new status If you need more explanation, I can make some code example for you