【发布时间】:2012-04-19 20:59:16
【问题描述】:
大家,感谢您的宝贵时间。
这是我的问题(这根本不是问题),有可能有一个继承自 ui.page 的类,然后实例化该类的一个对象并重定向到它?
类似这样的:
Public sub myMethod()
Dim myPage as new myClassThatInheritsFromUIPage()
Response.redirect(myPage) 'Here is one of my "no-idea" line
end sub
我在我的 webForm 中执行此操作(以及我想在继承自 ui.page 的类中执行此操作):
Response.BufferOutput = True
Response.ClearContent()
Response.ClearHeaders()
ostream=crReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
Response.AppendHeader("Cache-Control", "force-download")
Response.AppendHeader("Content-disposition","attachment;filename=ReportAsPDF.pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(ostream.ToArray())
Response.Flush()
HttpContext.Current.ApplicationInstance.CompleteRequest()
使用普通的 WebForm 完全可以实现我想要做的事情,但是我的 webForm 根本不呈现任何内容(至少作为 (x)html 如此,那是因为我想知道我要问的是有可能实现。
谢谢大家。
最后我只是将 "HttpContext.Current." 添加到包含“响应”属性的所有行中,所以现在我只有一个不继承自“UI.Page”的类" 并且只需调用清除缓冲区的方法(自定义方法),添加标题(强制下载,设置 mime 类型和文件名)并自行刷新响应并获得我想要的效果/使用它。
为了访问会话变量,只需添加“HttpContext.Current”。并且它有效,我不知道有多安全或是否是一种好方法,但似乎运作良好。
所以代码现在看起来像这样:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.ReportSource
Namespace foo
Public Class requestReport
'just to instance the object'
Public Sub New()
End Sub
Public Sub downloadReport()
'do all the stuff to connect and load the report'
HttpContext.Current.Response.BufferOutput = True
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.ClearHeaders()
ostream=crReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
HttpContext.Current.Response.AppendHeader("Cache-Control", "force-download")
HttpContext.Current.Response.AppendHeader("Content-disposition","attachment;filename=ReportAsPDF.pdf")
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.BinaryWrite(ostream.ToArray())
HttpContext.Current.Response.Flush()
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Sub
End Class
End Namespace
例如,通过命令执行以下操作:
dim myReport as new foo.requestReport()
myReport.downloadReport()
当然现在你可以根据需要添加更多的属性或方法。
所以现在我什至不不使用 Response.redirect() 或从“UI.Page”继承,只是一个“改变”“当前响应”和“刷新”的类“使用水晶报告即时创建的内容,我想我有点迷失了,但你的回答真的帮助了我,尤其是 Tejs 所说的,我刚刚做的几乎相同或相同.谢谢。
更新: 顺便说一句,我刚刚意识到 ReportDocument 类有这个方法:ExportToHttpResponse,它让我们将 Crystal Report 作为 PDF/XSL 等强制(或不强制)下载文件到浏览器。
【问题讨论】:
-
你在正确的轨道上。试试吧!
标签: asp.net code-behind response.redirect