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



我试图让一个按钮从文本框中获取唯一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();
        }

我不确定,但可能您需要指定类似的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();
        }

最新更新