从SSIS调用SSRS:某些PDF文件已损坏

时间:2018-05-08 10:14:40

标签: vb.net reporting-services ssis script-task

我从SSRS拨打SSIS Script task并以PDF格式存储报告。

以下是我在脚本任务中的代码:

 Protected Sub SaveFile(ByVal url As String, ByVal localpath As String)
    Dim loRequest As System.Net.HttpWebRequest
    Dim loResponse As System.Net.HttpWebResponse
    Dim loResponseStream As System.IO.Stream
    Dim loFileStream As New System.IO.FileStream(localpath, System.IO.FileMode.Create, System.IO.FileAccess.Write)
    Dim laBytes(256) As Byte
    Dim liCount As Integer = 1
    Try
        loRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
        loRequest.Credentials = System.Net.CredentialCache.DefaultCredentials
        loRequest.Timeout = 7200
        loRequest.Method = "GET"
        loResponse = CType(loRequest.GetResponse, System.Net.HttpWebResponse)
        loResponseStream = loResponse.GetResponseStream
        Do While liCount > 0
            liCount = loResponseStream.Read(laBytes, 0, 256)
            loFileStream.Write(laBytes, 0, liCount)
        Loop
        loFileStream.Flush()
        loFileStream.Close()
    Catch ex As Exception
    End Try
End Sub

Public Sub Main()
    Dim url, destination, Todaydate, FolderName, DestinationError As String
    Try

        Todaydate = Dts.Variables("TodayDate").Value.ToString

        FolderName = Dts.Variables("FolderName").Value.ToString

        destination = Dts.Variables("DestFolder").Value.ToString + "\" + Todaydate + "\" + FolderName + "\" + Dts.Variables("CurrentReport").Value.ToString + "_" + Dts.Variables("CurrentParamID").Value.ToString + "_" + Format(Now, "yyyyMMdd") + ".pdf"
        url = "http://server-name/ReportServer?/ReportPath/AUTOMATED/" + Dts.Variables("CurrentReport").Value.ToString + "&rs:Command=Render&Param=" + Dts.Variables("CurrentParamID").Value.ToString + "&rs:Format=PDF"   

        Dts.Variables("User::GeneratedPDFpath").Value = destination

        SaveFile(url, destination)

    End Try
    Dts.TaskResult = ScriptResults.Success

End Sub

此代码正常运行并以PDF格式生成报告。但是,有时它会生成大小为0 KB的PDF文件。打开该PDF时,它会显示“损坏的文件”错误。

P.S。我使用了存储结果集的执行SQL任务。在Foreach Loop容器中使用该结果集,它最初为今天的日期创建了文件夹,并将相应的PDF存储在文件夹中。

如果有人可以提供一些帮助,我们将不胜感激。

0 个答案:

没有答案