Response.Appendheader无法正常工作

时间:2014-07-15 16:28:34

标签: c# asp.net download

以下是我使用谷歌浏览器浏览器将文件从网络服务器下载(保存)到客户端计算机的代码。服务器上的文件是pdf文件。我尝试使用Response.AppendHeader& Response.Appendheader。它不会给我带来任何错误,也不会向我显示任何对话框或不下载文件。非常感谢。

        string pdfPathGLSummary = @"c:\\inetpub\\wwwroot\\sueTesting\\App_Data\\\GlSummary.pdf";
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.ClearHeaders();
        System.Web.HttpContext.Current.Response.Cookies.Clear();
        System.Web.HttpContext.Current.Response.ContentType = "Application/pdf";
        System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=GlSummary.pdf");
        System.Web.HttpContext.Current.Response.TransmitFile(@"c:\\inetpub\\wwwroot\\sueTesting\\App_Data\\\GlSummary.pdf");
        System.Web.HttpContext.Current.Response.Flush();
        System.Web.HttpContext.Current.Response.End();

1 个答案:

答案 0 :(得分:2)

您为文件名指定的字符串以@为前缀,表示它是verbatim string,因此包含了多个反斜杠。

@"c:\\inetpub\\wwwroot\\sueTesting\\App_Data\\\GlSummary.pdf"

应更改为以下其中一项:

@"c:\inetpub\wwwroot\sueTesting\App_Data\GlSummary.pdf"
"c:\\inetpub\\wwwroot\\sueTesting\\App_Data\\GlSummary.pdf"

请注意代码中的三重反斜杠。

相关问题