FileStream Write in VB.NET 生成损坏的 PDF



我正在从ssrs下载二进制文件,并将字节数组写入PDF文件。然后邮寄PDF文件。我收到电子邮件。但是,我在打开附件时会遇到错误。错误是" Adobe Acrobat Reader DC无法打开PDF文件,因为它不是受支持的文件类型或文件已损坏(例如,它是作为电子邮件发送的,并且无法正确解码。"

>

下面是编写PDF文件的代码。这也偶尔会发生。不是所有的时间。

Public Sub ExecuteReport()
        Try
            ' Reset Status Flags to initial value of True
            StatusVals(0) = True : StatusVals(1) = True : StatusVals(2) = True
            ' Call the GetBinary Method to download the requested report
            DownloadBinary = GetBinary(
                                                    SetConfigDetails.ReportURLPrefix & Report.gsHyperlink & "&db=" & SetConfigDetails.ReportDBId,
                                                    SetConfigDetails.ReportServerUser,
                                                    SetConfigDetails.ReportServerPassword,
                                                    StatusVals
                                               )
            SyncLock _fileLocker
                If StatusVals(0) Then
                    ' Generate a file path for the export with the file name as well; 
                    FileName = SetFSODetails.TempFolderPath & "" & Report.gsReportName
                    ' Call the WriteBinary Procedure to create an export from the downloaded response
                    Call WriteBinary(DownloadBinary, FileName, StatusVals)
                    If StatusVals(1) Then
                        ' Call the MailBinary Method to send the exports to all recepients; Changed on the 17th Dec 09
                        Mail.MailBinary(
                                                            SetConfigDetails.MailSMTP,
                                                            SetConfigDetails.MailSMTPPort,
                                                            SetConfigDetails.MailSMTPOnLocalOrRemote,
                                                            SetConfigDetails.MailFrom,
                                                            Report.gsEmail,
                                                            SetConfigDetails.MailBody,
                                                            Report.gsEMailSubject,
                                                            SetConfigDetails.MailCC,
                                                            SetConfigDetails.MailBCC,
                                                            FileName,
                                                            StatusVals,
                                                            ReportNumber,
                                                            Report.gsBatchId,
                                                            SubBatchNumber
                                                        )
                        If StatusVals(2) Then
                            DAccess.UpdateEMailFlag(Report.gsRowId.ToString)
                            Console.WriteLine("Record (" & Report.gsRowId.ToString & ") with Report Number " & ReportNumber.ToString & " Batch No " & Report.gsBatchId & " Sub Batch No " & SubBatchNumber.ToString() & " Processed Successfully...")
                            ' Delete the File once it has been mailed
                            Try
                                Dim FileInfo As New System.IO.FileInfo(FileName)
                                FileInfo.Delete()
                            Catch ex As Exception
                                Console.WriteLine("Error encountered with Report Number " & ReportNumber.ToString() & " with ReportName " & ReportName.ToString() & " : " & ex.Message)
                                Call StartUp.LogMe("Module1.Main.FileInfo", ex.StackTrace, ex.Message)
                            End Try
                        End If
                    End If
                End If
            End SyncLock
        Catch ex As Exception
            Console.WriteLine("Error encountered with Report Number " & ReportNumber.ToString() & " with ReportName " & ReportName.ToString() & " : " & ex.Message)
            Call StartUp.LogMe("Module1.Main.FileInfo", ex.StackTrace, ex.Message)
        End Try
    End Sub
Public Sub WriteBinary(ByVal Binary() As Byte, ByVal FileName As String, ByRef Status() As Boolean)
        Dim FStream As FileStream
        Try
            If Directory.Exists(FileName) = False Then
                Directory.CreateDirectory(Path.GetDirectoryName(FileName))
            End If
            FStream = New FileStream(FileName, FileMode.CreateNew)
            FStream.Write(Binary, 0, Binary.Length)
            FStream.Close()
        Catch ex As Exception
            ' Explicitly set the status to False
            Status(1) = False
            Console.WriteLine("Error encountered with Report Number " & ReportNumber.ToString() & " with ReportName " & ReportName.ToString() & " : " & ex.Message)
            Call StartUp.LogMe("Module1.WriteBinary", ex.StackTrace, ex.Message)
        End Try
    End Sub

好的,听起来像这个确切的问题 - 正在为整个文件大小编写" 0"。这是我现在有效的代码:

Dim dr1 as OracleDataReader
Dim fs as System.IO.FileStream
while dr1.read()
    Dim b(dr1.GetBytes(0,0,Nothing,0,Integer.MaxValue)-1 as Byte
   dr1.getbytes(0,0,b,0,b.Length)
   fs = new system.io.filestream("filelocationfilename.pdf",IO.FileMode.Create, IO.fileaccess.write)
   fs.write(b.0.b.Length)
   fs.close()
End While

这与多线程无关。这是因为SSRS超时正在返回错误。在代码中,GetBinaryMethod正在计时。它给出了错误响应。因此,PDF损坏了。

最新更新