我做了一个代码来生成PDF格式的Crystal Reports报告......但它在用户的同一页面中打开,并进行了搜索并单击了按钮...有任何方法可以在新标签或页面中打开PDF ?
我的代码是:
private void OpenPDF() { ReportDocument Rel = new ReportDocument(); Rel.Load(Server.MapPath("../Reports/Test.rpt")); BinaryReader stream = new BinaryReader(Rel.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.BinaryWrite(stream.ReadBytes(Convert.ToInt32(stream.BaseStream.Length))); Response.Flush(); Response.Close(); }谢谢您的帮助!
I did a code to generate a report of Crystal Reports in PDF...But it opens in the same page of the user did a search and clicked in the button...Have any ways to open the PDF in a new tab or page ?
My code is:
private void OpenPDF() { ReportDocument Rel = new ReportDocument(); Rel.Load(Server.MapPath("../Reports/Test.rpt")); BinaryReader stream = new BinaryReader(Rel.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.BinaryWrite(stream.ReadBytes(Convert.ToInt32(stream.BaseStream.Length))); Response.Flush(); Response.Close(); }Thanks for the help!
最满意答案
在最简单的解释中,要打开一个新的窗口或选项卡,页面的超链接应该将target属性设置为"_blank" 。
<a href="GeneratePDF.aspx" target="_blank">Link to open PDF in new window</a>或者你可以创建一些Javascript来打开一个新窗口。 确保你在页面的某个地方调用Javascript函数。
<script type="text/javascript"> function loadPDF() { window.open('GeneratePDF.aspx','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'); } </script>或者这段代码会通知网络浏览器该文件是一个下载文件(而不是浏览器窗口内的页面)。 我认为这是最好的方法,因为用户可以选择打开或保存PDF。 所以这不符合你的要求,但你可能认为它更好。
private void OpenPDF(string downloadAsFilename) { ReportDocument Rel = new ReportDocument(); Rel.Load(Server.MapPath("../Reports/Test.rpt")); BinaryReader stream = new BinaryReader(Rel.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment; filename=" + downloadAsFilename); Response.AddHeader("content-length", stream.BaseStream.Length.ToString()); Response.BinaryWrite(stream.ReadBytes(Convert.ToInt32(stream.BaseStream.Length))); Response.Flush(); Response.Close(); }In its most simplest interpretation, to open a new window or tab, the hyperlink to the page should have the target attribute set to "_blank".
<a href="GeneratePDF.aspx" target="_blank">Link to open PDF in new window</a>Or you could create some Javascript that opens a new window instead. Make sure you call the Javascript function somewhere on the page.
<script type="text/javascript"> function loadPDF() { window.open('GeneratePDF.aspx','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'); } </script>Or this code will inform the web browser that the file is a download (rather than a page to view inside the browser window). I think this is the best approach because the user gets the choice of Opening or Saving the PDF. So this does not do what you're asking for, but you might think it's better.
private void OpenPDF(string downloadAsFilename) { ReportDocument Rel = new ReportDocument(); Rel.Load(Server.MapPath("../Reports/Test.rpt")); BinaryReader stream = new BinaryReader(Rel.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment; filename=" + downloadAsFilename); Response.AddHeader("content-length", stream.BaseStream.Length.ToString()); Response.BinaryWrite(stream.ReadBytes(Convert.ToInt32(stream.BaseStream.Length))); Response.Flush(); Response.Close(); }更多推荐
发布评论