Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pdf viewing in asp.net gives File Download window in the web server, b

Status
Not open for further replies.

svkumsani

Programmer
Feb 2, 2006
27
0
0
US
I am new to .net development and I am working on some reports where the user needs to have web printing option. Since the report requires column headers in all the pages, I have decided to use crystal reports instead of using window.print(). What I learned is to do web printing using crystal reports, I need to export it to pdf file and stream it to the user. Herewith I am providing the code I am using

'Dim oStream As New System.IO.MemoryStream

oRpt.SetDataSource(dt)
myExportFile = "c:\temp\" & Session.SessionID.ToString & ".pdf"


'Response.BinaryWrite(oXMLHttp.ResponseBody)

Response.WriteFile(myExportFile)
'Response.End()
'Response.WriteFile(myExportFile)
'Response.Redirect(myExportFile)
Response.Flush()
Response.Close()
System.IO.File.Delete(myExportFile)

The code works fine in my local pc, but it gives File download window showing Open, Save, Cancel option when I deploy it into a web server. I tried to launch my website using Firefox or Netscape Navigator, the website works fine. I am not sure what I am missing in my web server to make the IE work without showing the File Download window.

Your help will be greatly appreciated if anyone throw some light in this problem
 
The file open/save is an option that is set on the client and therefore you have no control over it.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
What I am saying is just to open an aspx file in which I am streaming pdf file, gives File download option. Meaning File download window is showing it for aspx file.

Since I can't copy and paste the image, herewith I am showing what I am getting in Filedownload window

Name : DailyVolMeter.aspx
Type: Unknown File Type
From: xyz.com
 
heres how I display a pdf without the Open or download option

Public Sub ExportData(ByRef oRpt As Object)
Dim fs As IO.FileStream
Dim FileSize As Long
Dim oDest As New CrystalDecisions.Shared.DiskFileDestinationOptions
Dim ExportFileName As String = Server.MapPath("/") & ConfigurationSettings.AppSettings("ExportDir") & Session.SessionID & ".pdf"
Try
oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
oRpt.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat
oDest.DiskFileName = ExportFileName
oRpt.ExportOptions.DestinationOptions = oDest
oRpt.Export()
'Build Target Filename
'Send the file to the user that made the request
Response.Clear()
Response.Buffer = True
Response.AddHeader("Content-Type", "application/pdf")
fs = New IO.FileStream(ExportFileName, IO.FileMode.Open)
FileSize = fs.Length
Dim bBuffer(CInt(FileSize)) As Byte
fs.Read(bBuffer, 0, CInt(FileSize))
fs.Close()
Response.BinaryWrite(bBuffer)
Response.Flush()
Response.Close()
Catch e As Exception
End Try
End Sub

 
just another note looking at your code. I think you are getting the open, save and cancel option is because you are not specifying what to do with the pdf in your code. so by default you get those options. I display CR in odf all the time using the code I gave you plus using the save, open and cancel option.

using this line
Response.AddHeader("Content-Disposition", "attachment;filename=" & Session.SessionID & ".pdf;")

will give you the save,open cancel options.

 
Thanks a bunch dvannoy. I have been breaking my head for more than a week for this problem and thanks for your solution. Appreicate it.

Another quick question for you. In my code I am exporting the crystal report into a pdf file. Right now I am default it to c:\temp. Is there a way to hardcode it to my server's c: drive temp folder. If so, how to do that.
 
you should not have to change c:\temp..

just replace server.mappath to c:\temp..that should do it.

 
Like you have mentioned instead of using c:\temp\DailyVolMeter.pdf, I tried the following

myExportFile = Server.Mappath("\DailyVolMeter.pdf")

This works fine in my local pc, but I am getting an error in the web server. Do you have any idea what am I doing wrong?
 
I think so, here is the error message for your referenece

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: CrystalDecisions.CrystalReports.Engine.LoadSaveReportException: Error in File C:\WINDOWS\TEMP\temp_5891e38a-3491-4d3b-b5fe-ef5c413a78e6.rpt: Access to report file denied. Another program may be using it.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[LoadSaveReportException: Error in File C:\WINDOWS\TEMP\temp_5891e38a-3491-4d3b-b5fe-ef5c413a78e6.rpt:
Access to report file denied. Another program may be using it.]
.F(String  , EngineExceptionErrorID 
) +272
.A(Int16 , Int32 ) +537
CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext) +469
CrystalDecisions.CrystalReports.Engine.FormatEngine.Export() +107
CrystalDecisions.CrystalReports.Engine.ReportDocument.Export() +67
bridgeline.DailyVolMeter.btnPrint_Click(Object sender, EventArgs e) in c:\inetpub\ System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292
 
try using
Server.MapPath("/") & ConfigurationSettings.AppSettings("ExportDir") & Session.SessionID & ".pdf"

instead and see what happens

 
Thanks a lot dvannoy, it works great now. Have a nice weekend.
 
dvannoy said:
heres how I display a pdf without the Open or download option
It will still be up to the users environment how the file is handled. In windows explorer, if you go to:

Tools -> Options -> File Types

Scroll down to the PDF entry (which the environment knows the file will be as you set the content-type to "application/pdf") and click the advanced tab. There will be a checkbox named "Confirm open after download". This checkbox defines what will happen on the users machine and as such you have no control over what will happen and it may still ask them if they want to open or save it.

I don't know how other operating systems handle this though, although I expect there will be a similar option.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm, you are correct but I think by default in xp that's not checked.

 
Hi, I am getting a different problem now. since my template crystal report file worked correctly. I created my actual file using a .ttx(field definition) file. It is giving me Query Engine Error, could anyone of you throw some light here?

Here is my sample code

'this is declared in the beginning of my code and DailyVolMeterRpt.rpt is the new crystal file created and it is included in the project
Public oRpt As New DailyVolMeterRpt

Inside print button
oRpt.Load()
'I am using some parameter fields
crParameterFieldDefinitions = oRpt.DataDefinition.ParameterFields
'for right now I am storing the table into a session. I know it is not a good way to do it, but for testing purpose I am doing it.
dt = CType(Session("DV_Source"), DataTable)
oRpt.SetDataSource(dt)

myExportFile = Server.MapPath("/") & ConfigurationSettings.AppSettings("ExportDir") & Session.SessionID & ".pdf"

myDiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions
myDiskFileDestinationOptions.DiskFileName = myExportFile
myExportOptions = oRpt.ExportOptions

'myExportOptions = ReportDocument1.ExportOptions
With myExportOptions
.DestinationOptions = myDiskFileDestinationOptions
.ExportDestinationType = .ExportDestinationType.DiskFile
.ExportFormatType = .ExportFormatType.PortableDocFormat
End With


oRpt.Export()

I am getting an error in export.
 
that sub I gave you is the export part only. this sub cannot execute without displaying the report. how are you displaying the report? show your code from the form that has the viewer on it.

 
do I have to display the report? what I need to do is to export the file into pdf and show it to the user for printing. I have an existing code which uses datagrid for displaying the data. The above code I mentioned it in my prev message which shows the code I am using.
 
if your using a datagrid to display the data why are you trying to use Crystal???

what you need to do is export the grid to pdf. you are getting the two mixed up.

 
The main reason I am doing the crystal is to show the column headers on each and every pages. I learned that by using crystal reports the only way to printing is to export it into pdf file. If I can achieve the same result by converting the grid to pdf, I will be more happy to do that. How can I export the grid to pdf file. More over in my report user selected entries need to go along with the report. I am using parameter fields in the crystal reports for that. How to do that if I export the grid to pdf file. Thanks for your help.
 
the export sub I gave you exports the report AFTER it gets created by crystal. it will not show the report but it still needs to go through the steps as if it was going to be shown. in order to do what you want, you will need to use the PUSH method. which is, create a Dataset, drop a Crystal viewer onto a form, then export your report to pdf. you are missing many steps here. do you have a form with a viewer on it?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top