2017-03-02 106 views
0

我從SSRS下載二進制文件並將字節數組寫入PDF文件。 然後郵寄PDF文件。我收到電子郵件。但是,打開附件時出現錯誤。錯誤是「Adobe Acrobat Reader DC無法打開PDF文件,因爲它既不是受支持的文件類型,也不是文件已損壞(例如,它是作爲電子郵件發送的,未正確解碼。」VB.NET中的FileStream寫入產生損壞的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 
+1

您需要展示更多的代碼 - 對於從中獲得'Binary'的初學者以及如何將其傳遞給方法。您目前向我們顯示的代碼不會破壞文件。 –

+0

@VisualVincent - 根據您的建議添加更多代碼。 – SaiBand

+0

假設你從'GetBinary'得到的是不完整的。是否有其他方式可以下載文件,以便您可以使用它檢查應用程序執行時丟失了多少字節? –

回答

0

這與無關w ith多線程。這是因爲SSRS超時會返回錯誤。在代碼中GetBinaryMethod超時。它給出了一個錯誤響應。因此,PDF已損壞。

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 
+0

@Breettvd,不知道我是否理解正確。 – SaiBand

+0

這甚至不會編譯,並且當它仍然不斷覆蓋同一個文件時。 –