【发布时间】:2014-06-25 06:52:37
【问题描述】:
我想通过单击按钮下载 PDF 文件。我试过这样:
protected void lbtnDownload_Click(object sender, EventArgs e)
{
if (lbtnDownload.Text != string.Empty)
{
else if (lbtnDownload.Text.EndsWith(".pdf"))
{
Response.ContentType = "application/pdf";
}
string filePath = lbtnDownload.Text;
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Response.TransmitFile(HttpContext.Current.Server.MapPath("~/") + "\\PDF\\" + filePath );
Response.End();
}
}
但是什么都没有发生,我的意思是当我调试它时没有异常,但是文件没有下载。
有谁知道我的错误在哪里,我应该怎么做才能下载 PDF?
【问题讨论】:
-
文件应该从哪里下载?它是 UAC-Path(如
\\Computername\anyshare\anyfile.extension)还是 HTTP 服务器? (如anysite.tld/file.ext或127.0.0.1/file.ext) -
它是一个 UAC-Path 而不是 HTTP 服务器
-
尝试在
Response.End();之前添加Response.Flush(); -
我在控制台中收到此错误:- 未捕获的 Sys.WebForms.PageRequestManagerParserErrorException:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。
-
我修好了。问题出在我使用的 UpdatePanel 上。我添加了一个 PostBackTrigger,现在一切正常。谢谢