c#生成报告并下载pdf按钮

时间:2015-02-05 15:24:42

标签: c# pdf report server

我尝试使按钮从文本框中获取唯一ID,并从位于现有报告服务器上的.rdl文件生成并下载pdf报告。现在,代码将连接到报告服务器并下载具有正确名称的.pdf文件,但是当我尝试打开它时,它说它已损坏。下面是我在按钮点击事件中的所有代码。任何帮助,将不胜感激。谢谢!


try
        {
            byte[] PDF;
            WebClient myWebClient = new WebClient();
            //define the credentials
            NetworkCredential networkCredential = new NetworkCredential(ConfigurationManager.AppSettings["ReportServerUser"].ToString(), ConfigurationManager.AppSettings["ReportServerPass"].ToString());
            myWebClient.Credentials = networkCredential;
            string strURL = string.Empty;

            strURL = ConfigurationManager.AppSettings["ReportServerURL"].ToString() + "/Pages/Folder.aspx?ItemPath=%2fDev+Reports%2f404a5+Participant+Notice_TD" + "?planid=" + txt404a5.Text + "&rs:Command=Render&rs:Format=PDF";
            HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(strURL);
            myWebRequest.Credentials = networkCredential;
            myWebRequest.Timeout = 600000;
            HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
            System.IO.Stream myStream = myWebResponse.GetResponseStream();
            byte[] Buffer = new byte[4096];
            int BlockSize = 0;
            System.IO.MemoryStream TempStream = new System.IO.MemoryStream();
            do
            {
                BlockSize = myStream.Read(Buffer, 0, 4096);
                if (BlockSize > 0)
                    TempStream.Write(Buffer, 0, BlockSize);
            } while (BlockSize > 0);
            PDF = TempStream.ToArray();
            myWebClient.Dispose();

            //Send the report to the browser
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-disposition", "attachment; filename=SummaryOfPlanExpenses.pdf");
            Response.BinaryWrite(PDF);

            Response.End();
        }catch (WebException webEx){
            test.Text = webEx.ToString();
        }

1 个答案:

答案 0 :(得分:0)

我不确定,但也许您需要指定pdf的编码,如

  Response.ContentEncoding = new UTF8Encoding();

try
        {
            byte[] PDF;
            WebClient myWebClient = new WebClient();
            //define the credentials
            NetworkCredential networkCredential = new NetworkCredential(ConfigurationManager.AppSettings["ReportServerUser"].ToString(), ConfigurationManager.AppSettings["ReportServerPass"].ToString());
            myWebClient.Credentials = networkCredential;
            string strURL = string.Empty;

            strURL = ConfigurationManager.AppSettings["ReportServerURL"].ToString() + "/Pages/Folder.aspx?ItemPath=%2fDev+Reports%2f404a5+Participant+Notice_TD" + "?planid=" + txt404a5.Text + "&rs:Command=Render&rs:Format=PDF";
            HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(strURL);
            myWebRequest.Credentials = networkCredential;
            myWebRequest.Timeout = 600000;
            HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
            System.IO.Stream myStream = myWebResponse.GetResponseStream();
            byte[] Buffer = new byte[4096];
            int BlockSize = 0;
            System.IO.MemoryStream TempStream = new System.IO.MemoryStream();
            do
            {
                BlockSize = myStream.Read(Buffer, 0, 4096);
                if (BlockSize > 0)
                    TempStream.Write(Buffer, 0, BlockSize);
            } while (BlockSize > 0);
            PDF = TempStream.ToArray();
            myWebClient.Dispose();

            //Send the report to the browser
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-disposition", "attachment; filename=SummaryOfPlanExpenses.pdf");

            Response.ContentEncoding = new UTF8Encoding();

            Response.BinaryWrite(PDF);

            Response.End();
        }catch (WebException webEx){
            test.Text = webEx.ToString();
        }