【发布时间】:2018-02-20 19:18:14
【问题描述】:
在我的 webform 项目中的一个表单中,有一个用户控件。当用户单击此用户控件中的“下载”按钮时,这就是我想要做的:
- 下载文件
- 更改数据库中的相关记录(用于更改用户状态)
- 重新加载该用户控件(或整个表单)以根据新状态显示信息。
我尝试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;
}
}
有什么解决办法吗?
【问题讨论】:
-
下载开始后无法刷新页面,因为下载开始时服务器已经发送了标头,您唯一的选择是使用一些javascript我建议如下: 1 - 更改数据库中的相关记录(用于更改用户状态) 2 - 使用 javascript 在新窗口中下载文件(请参阅
ScriptManager.RegisterStartupScript来实现) 3 - 刷新页面中的数据以显示新状态如果您需要更多解释,我可以为您制作一些代码示例 -
太棒了!它有效
标签: c# httphandler