在我的代码中,我想在下载pdf后重定向到一个thankyou.html页面,这样我就可以用这种方法编写代码
protected void download()
{
try
{
string path = Server.MapPath("pdf/myDoc.pdf");
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
string strURL = path;
WebClient req = new WebClient();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment;filename=\"myDoc.pdf\"");
byte[] data = req.DownloadData(path);
Response.BinaryWrite(data);
Response.Flush();
Response.Redirect("thankyou.html");
}
else
{
Response.Write("This file does not exist.");
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('" + ex.Message.ToString() + "');", true);
}
}但它显示了错误无法计算表达式,因为代码经过优化,或者本机框架位于调用堆栈的顶部,并且当Response.Redirect("thankyou.html");外部显示错误无法在发送HTTP头后重定向。能指导我如何重定向到另一个页面吗?
发布于 2018-05-14 13:04:11
发送文件和重定向到页面是两个不同的HTTP响应。你不能把它们结合起来。
通常情况下,下载是由您返回的页面触发的,所以在显示感谢消息时,有一些javascript来处理下载。如果愿意,可以使用以下javascript:Download File Using Javascript/jQuery。
https://stackoverflow.com/questions/50331014
复制相似问题