2012-04-18 101 views
1

我创建了一个PDF,我试图在新窗口中打开它(或新标签,但我现在工作的新窗口)打开PDF在新页面

这里是我的aspx代码。链接按钮位于面板中的GridView中,该面板位于更新面板中(是的,我看到有人遇到更新面板和此问题,但我必须这样做)。

<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> 

这里是我的javascript打开的新窗口

function LoadPDF() { 
    window.open('myPDF.aspx', '', 
    'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'); 
} 

这里是C#。第一个片段是点击链接按钮时调用的内容(在javascript之后)。第二种是实际创建PDF并尝试将其发送到新窗口的方法。

{ 
    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(); 
    } 
} 

在此先感谢

UPDATE:

后通过我们的旧代码多挖我发现周围的工作。几乎所有你们给我的代码都已经在我们的代码中,我只需要弄清楚如何使用我们的代码。我结束了使用我的LoadPDF函数,但不得不传递一个字符串参数,因为URL使用“?=”,我必须填写。我感谢所有的帮助!

回答

1

为什么需要通过javascript来完成?

会一

<a href="myPDF.aspx" target="_blank">blah</a> 

做的伎俩?

编辑

我做了一些研究,并且正在使用Request.Close()不正确。根据the documentation,该方法“不适用于正常的HTTP请求处理”。

也许你应该用Request.End()来代替。

更改该行应该修复它,但我也考虑改变

 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(); 

到更多的东西一样

 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(); 

但是,如果它的工作原理,它的工作原理。

+0

看来这个问题是产生实际的PDF文件,而不是开放的浏览器窗口(并没有提到阿贾克斯后btw)。 – BluesRockAddict 2012-04-18 21:06:52

+0

@BluesRockAddict谢谢,我已经更新了我的答案。 ;) – mayhewr 2012-04-18 21:11:02

+0

我也试过这个,它打开新选项卡很好,但我的PDF仍未显示。我认为我的主要问题是让我的PDF显示。 – 2012-04-19 17:23:43

1

尝试增加内容处置/ Content-Length的头给你的回应:

Response.AddHeader("Content-Disposition", "inline; filename=\"report.pdf\""); 
Response.AddHeader("Content-Length", Bin.BaseStream.Length); 
+0

这将导致下载PDF,而不是在新标签中打开。你也有你的代码中的拼写错误(我没有编辑它的代表)。 – mayhewr 2012-04-18 21:01:38

+0

谢谢,我已经更新了我的答案。 – BluesRockAddict 2012-04-18 21:05:25

+0

我试过了你的建议,它似乎没有帮助,因为它仍然在做同样的事情。 – 2012-04-19 17:20:28