我從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
您需要展示更多的代碼 - 對於從中獲得'Binary'的初學者以及如何將其傳遞給方法。您目前向我們顯示的代碼不會破壞文件。 –
@VisualVincent - 根據您的建議添加更多代碼。 – SaiBand
假設你從'GetBinary'得到的是不完整的。是否有其他方式可以下載文件,以便您可以使用它檢查應用程序執行時丟失了多少字節? –