Reputation: 15
I have created a PDF and I am trying to open it in a new window (or new tab, but I have the new window working right now)
Here is my aspx code. The link button is in a GridView which is in a Panel, which is in an Update Panel (Yeah, I've seen people having problems with Update panels and this, but I have to do it this way).
<ItemTemplate>
<asp:LinkButton ID="ReportLink" runat="server" CommandName="GenReport"
CommandArgument='<%# Container.DataItemIndex %>'
OnClientClick="LoadPDF()" PostBackUrl="~/Indices/myPDF.aspx"
Text="View Report" ToolTip="Genereate a PDF of the Report"></asp:LinkButton>
</ItemTemplate>
Here is my javascript that opens the new window
function LoadPDF() {
window.open('myPDF.aspx', '',
'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
}
and here is the C#. The first snippet is what gets called when the linkbutton is clicked (after the javascript). The second is the method that actually creates the PDF and attempts to send it to the new window.
{
DataTable dt = (DataTable)Session["IndexTable"]; //copy of the GirdView
int rowIndex = Convert.ToInt32(e.CommandArgument.ToString());
DataRow row = dt.Rows[rowIndex];
GenerateReport(row["ITEM_ID"].ToString());
}
private void GenerateReport(string itemID)
{
OracleConnection connection = new OracleConnection(connstr);
try
{
connection.Open();
//code here omitted, all sql and setting up info for the doc
// giving TurnOverData a dataset
ReportDocument doc = new ReportDocument();
doc.FileName = Server.MapPath("myRpt.rpt");
doc.SetDataSource(TurnoverData);
//here adding parameters to doc
System.IO.MemoryStream stream = (System.IO.MemoryStream)doc.ExportToStream(ExportFormatType.PortableDocFormat);//leftover code from previous functionality (i dont think it is doing anything)
BinaryReader Bin = new BinaryReader(doc.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat));
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType="application/pdf";
Response.BinaryWrite(Bin.ReadBytes(Convert.ToInt32(Bin.BaseStream.Length)));
Response.Flush();
Response.Close();
}
catch (Exception ex)
{
Logger.logError("GenerateReport() " + ex.Message);
Utilities.SendAnAlert(this, "An error occurred while generating PDF file and the file could not be displayed.");
}
finally
{
connection.Close();
connection.Dispose();
}
}
Thanks in advance
UPDATE:
After much digging through our old code I found a work around. Nearly all of the code you guys suggested to me was already in our code, I just needed to figure out how to use what we had. I ended up using my LoadPDF function but had to pass it a string argument because the url used"?= " that I had to fill in. I appreciate all the help!
Upvotes: 1
Views: 6210
Reputation: 4021
Why does this need to be done via javascript?
Would a
<a href="myPDF.aspx" target="_blank">blah</a>
do the trick?
EDIT
I did some research, and you are using Request.Close()
incorrectly. According to the documentation, that method is "not intended for normal HTTP request processing".
Perhaps you should use Request.End()
instead.
Changing that line should fix it, but I would also consider changing
System.IO.MemoryStream stream = (System.IO.MemoryStream)doc.ExportToStream(ExportFormatType.PortableDocFormat);//leftover code from previous functionality (i dont think it is doing anything)
BinaryReader Bin = new BinaryReader(doc.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat));
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType="application/pdf";
Response.BinaryWrite(Bin.ReadBytes(Convert.ToInt32(Bin.BaseStream.Length)));
Response.Flush();
to something more like
System.IO.MemoryStream stream = (System.IO.MemoryStream)doc.ExportToStream(ExportFormatType.PortableDocFormat);
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType="application/pdf";
stream.CopyTo(Response.OutputStream);
Response.Flush();
But if it works, it works.
Upvotes: 1
Reputation: 15683
Try adding Content-Disposition/Content-Length headers to your response:
Response.AddHeader("Content-Disposition", "inline; filename=\"report.pdf\"");
Response.AddHeader("Content-Length", Bin.BaseStream.Length);
Upvotes: 1