0

Im trying to make a button take a unique ID from a textbox and generate and download a pdf report from a .rdl file that lives on an existing reporting server. Right now the code will connect to reporting server and download a .pdf file with the correct name but when I try to open it is says its corrupt. Below is all the code I have in the button click event. Any help would be appreciated. Thanks!


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();
        }
  • ` Response.End();` before this line have you thought about explicitly calling `Response.Flush()` – MethodMan Feb 05 '15 at 15:33
  • I hadn't but I just did and unfortunately that wasn't the answer. – Markese Dixon Feb 05 '15 at 15:37
  • take a look at some of these options located here on this page. I just tested it creating a simple web application and I can view the `.pdf` on my end ..http://stackoverflow.com/questions/13779139/writing-memorystream-to-response-object – MethodMan Feb 05 '15 at 15:49

1 Answers1

0

I'm not sure but maybe you need to specify the encoding for pdf like

  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();
        }
MirlvsMaximvs
  • 1,453
  • 1
  • 24
  • 34