FileStream在VB.NET中写入会生成损坏的PDF文件

时间:2017-03-02 19:42:07

标签: vb.net

我正在从SSRS下载Binary并将字节数组写入PDF文件。 然后邮寄PDF文件。我收到了电子邮件。但是,我在打开附件时收到错误。错误是" Adob​​e Acrobat Reader DC无法打开PDF文件,因为它不是受支持的文件类型或文件已损坏(例如,它是作为电子邮件发送的,未被正确解码。&#34 ;

以下是编写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

2 个答案:

答案 0 :(得分:0)

好的,听起来像这个确切的问题 - 正在为整个文件大小写“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("\filelocation\filename.pdf",IO.FileMode.Create, IO.fileaccess.write)
   fs.write(b.0.b.Length)
   fs.close()
End While

答案 1 :(得分:0)

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