Export Crystal Report to PDF in a Loop only works with first

夙愿已清 提交于 2020-01-06 14:48:14

问题


i'm trying to generate a report and export it to pdf in a loop, the report will receive a new parameter in each loop and prompt the client to download a PDF, in other words, the client may need to download 2 or 3 (or more) PDFs at the same time, the problem is that the prompt to accept the download only appears for the first pdf, dont know why. I can export to disk (server side) without any problems.

Code:

Sub PrintReport(ByVal Cod As Integer)
        Dim CTableLogInfo As TableLogOnInfo
        Dim ConnInfo As CrystalDecisions.Shared.ConnectionInfo = New ConnectionInfo()
        ConnInfo.Type = ConnectionInfoType.SQL
        ConnInfo.ServerName = ConfigurationManager.AppSettings("SQLSERVERNAME")
        ConnInfo.DatabaseName = ConfigurationManager.AppSettings("SQLDBNAME")
        ConnInfo.UserID = ConfigurationManager.AppSettings("SQLSERVERUSER")
        ConnInfo.Password = ConfigurationManager.AppSettings("SQLSERVERPASSWORD")
        ConnInfo.AllowCustomConnection = False
        ConnInfo.IntegratedSecurity = False

        For Each CTable As Table In CrystalReportSource1.ReportDocument.Database.Tables
            CTable.LogOnInfo.ConnectionInfo = ConnInfo
            CTableLogInfo = CTable.LogOnInfo
            CTableLogInfo.ReportName = CrystalReportSource1.ReportDocument.Name
            CTableLogInfo.TableName = CTable.Name
            CTable.ApplyLogOnInfo(CTableLogInfo)
        Next

        Dim pField As ParameterField = CrystalReportSource1.ReportDocument.ParameterFields(0)
        Dim val1 As ParameterDiscreteValue = New ParameterDiscreteValue
        val1.Value = Cod
        pField.CurrentValues.Clear()
        pField.CurrentValues.Add(val1)


        Dim PDFName As String = "PDF Nº " & Cod 
        CrystalReportSource1.ReportDocument.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Page.Response, True, PDFName)      
    End Sub

EDIT: Tried to zip the reports with DotNetZip but i get an broken zip.

Can you tell me whats wrong? (Solved: code bellow is corrected now)

    Response.ClearContent()
    Response.ClearHeaders()
    Response.ContentType = "application/zip"
    Response.AppendHeader("content-disposition", "attachment; filename=AllPDFs.zip")

    Using zipFile As New ZipFile()
        For i = 0 To Cod.Length - 1
            If Cod(i) > 0 Then
                val1.Value = Cod(i)
                pField.CurrentValues.Clear()
                pField.CurrentValues.Add(val1)

                val2.Value = Cod(i)
                pField2.CurrentValues.Clear()
                pField2.CurrentValues.Add(val2)

                Dim PDFNameAs String = "PDF Nº " & Cod(i) & ".pdf" 

                Dim s As New System.IO.MemoryStream 
                s =CrystalReportSource1.ReportDocument.ExportToStream(ExportFormatType.PortableDocFormat)
                zipFile.AddEntry(PDFName, s)
            End If
        Next
        zipFile.Save(Response.OutputStream)
    End Using
    Response.Clear()

回答1:


Probably the response ends after the first one, therefore there's no response to write to for the 2nd and 3rd attempts.

Instead, you can have the client download the reports via AJAX Request (move your report generation into an .ashx generic handler), or have the user click the button 3 times to initiate new requests.

Or zip the PDF's up until a single file and allow the client to download that.



来源:https://stackoverflow.com/questions/28220649/export-crystal-report-to-pdf-in-a-loop-only-works-with-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!