首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在VB.NET中写入FileStream会产生损坏的PDF

在VB.NET中写入FileStream会产生损坏的PDF
EN

Stack Overflow用户
提问于 2017-03-03 03:42:07
回答 2查看 605关注 0票数 0

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

下面是编写PDF文件的代码。此外,这种情况偶尔也会发生。并不是一直都这样。

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-04 02:43:29

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

票数 0
EN

Stack Overflow用户

发布于 2017-03-03 03:54:08

好的,有听起来像这样的问题--为整个文件大小写“0”。下面是我现在可以使用的代码:

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42564514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档