下载文件后重新加载用户控件

时间:2018-02-20 19:18:14

标签: c# httphandler downloadfile

在我的webform项目的一个表单中,有一个用户控件。这就是我想要做的事当用户点击"下载"此用户控件中的按钮:

  1. 下载文件
  2. 更改db中的相关记录(更改用户状态)
  3. 重新加载该用户控件(或整个表单)以根据新状态显示信息。
  4. 我尝试this solution,但用户控件没有重新加载。这是onclick事件的主体:

      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();
    

    这是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;
            }
        }
    

    有没有解决方案?

1 个答案:

答案 0 :(得分:2)

下载开始后无法刷新页面,因为服务器已经在下载开始时发送了标题,您唯一的选择是使用一些javascript我建议如下: 1 - 更改db中的相关记录(用于更改用户状态) 2 - 使用javascript在新窗口中下载文件(请参阅ScriptManager.RegisterStartupScript以实现该目的) 3 - 刷新页面中的数据以显示新状态如果您需要更多解释,我可以为您制作一些代码示例

相关问题