【问题标题】:DOCX and XLSX document corruption when retrieving from SQL从 SQL 检索时 DOCX 和 XLSX 文档损坏
【发布时间】:2011-01-22 00:21:44
【问题描述】:

我在stackOverflow上找到了以下代码:

公共函数 retreiveDoc(ByVal docFileDescription As String, _ ByVal genericID 作为字符串,_ ByVal formName As String) As String

    Dim docPathandName As String = String.Empty

    Using connection As SqlConnection = New SqlConnection(My.Settings.connSQL)

        Dim command As SqlCommand = New SqlCommand( _
            "SELECT fileStore_ID, fileType, imageData FROM FileStore WHERE fileDescription = " & _
            "'" & docFileDescription & "'" & _
            " AND generic_ID = " & "'" & genericID & "'" & _
            " AND appFormName = " & "'" & formName & "'", connection)

        Dim docType As String = String.Empty

        '! Writes the BLOB to a file 
        Dim stream As FileStream

        '! Streams the binary data to the FileStream object.
        Dim writer As BinaryWriter

        '! The size of the BLOB buffer.
        'Dim bufferSize As Integer = 100
        Dim bufferSize As Integer = 4096

        '! The BLOB byte() buffer to be filled by GetBytes.
        Dim outByte(bufferSize - 1) As Byte

        '! The bytes returned from GetBytes.
        Dim retval As Long

        '! The starting position in the BLOB output.
        Dim startIndex As Long = 0

        '! The publisher id to use in the file name.
        Dim fileStore_ID As Int32

        '! Open the connection and read data into the DataReader.
        connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader(CommandBehavior.SequentialAccess)

        Do While reader.Read()
            fileStore_ID = reader.GetInt32(0)
            docType = reader.GetString(1)

            '! Create a file to hold the output.
            stream = New FileStream( _
              defaultDrive & "\imageData" & fileStore_ID & docType, FileMode.OpenOrCreate, FileAccess.Write)

            writer = New BinaryWriter(stream)

            '! Reset the starting byte for a new BLOB.
            startIndex = 0

            '! Read bytes into outByte() and retain the number of bytes returned.
            retval = reader.GetBytes(2, startIndex, outByte, 0, bufferSize)

            '! Continue while there are bytes beyond the size of the buffer.
            Do While retval > 0
                writer.Write(outByte)
                writer.Flush()

                ' Reposition start index to end of the last buffer and fill buffer.
                startIndex += retval
                retval = reader.GetBytes(2, startIndex, outByte, 0, bufferSize)

            Loop

            writer.Flush()
            writer.Close()
            stream.Close()
        Loop

        reader.Close()
        connection.Close()

        Return (defaultDrive & "imageData" & fileStore_ID & docType)

    End Using

End Function

它适用于除 DOCX 和 XLSX 文档之外的所有内容。我将原来的 C# 转换为 VB,也许我遗漏了一些东西。

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    检查写入部分是否过度分配缓冲区 Byte() 数组。另一个例子见我在this posthere 中的回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2020-01-03
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多