服务器无法HTTP标头后清晰头已被送往
问题描述:
为了下载我写了下面的代码服务器无法HTTP标头后清晰头已被送往
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)] public ActionResult Download(string fileName) {
try {
string filepath = @"C:\shadow_copy.rar";
Response.ClearHeaders();
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename= " + fileName.TrimEnd());
Response.AddHeader("Content-Length", fileName.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(filepath.TrimEnd());
Response.End();
}
catch (Exception exp)
{
//
}
return Redirect("http://mysite.com");
}
我也用
一个文件,如果(!Response.IsRequestBeingRedirected)//将不叫的Response.Redirect(“http://www.google.com”)
但它显示错误
“后,HTTP标头已被送往不能重定向。”
答
下载文件后无法重定向,您正试图执行2个操作,只能执行第一个操作。
我建议你在新的(弹出)窗口中下载文件并根据需要重定向主页面。
答
尝试这样的,这是一个容易一点:
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult Download(string fileName)
{
string filepath = @"C:\shadow_copy.rar";
return File(filepath, "application/octet-stream", "shadow_copy.rar");
}
而且你不能下载后重定向。
你想完成什么?下载文件或重定向用户。你必须选择一个到另一个。 – 2011-12-22 09:40:42