Click here to Skip to main content
15,029,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Self Hosted WCF service which is reading a big RAR file with binaryreader and returning bytearray to client

Service splitting file as parts and 10MB per parts( 10485860 Bytes)

I checking on debug mode for size of bytearray in WCF service before return and seeing 10485860 Bytes. So this is true size.

But client received 13981237 Bytes per parts, why is this difference.
This making my file is corrupt when i create clientside file with received bytes
This is my code from WCF

Dim my_InStream As System.IO.FileStream = Nothing
            'Dim i As Integer = 0

            my_InStream = New System.IO.FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim my_ibinaryreader As System.IO.BinaryReader = New System.IO.BinaryReader(my_InStream)
            Dim my_splitArr As New ArrayList

            'Dim ofile As FileInfo = New FileInfo(File)

            Dim ofilesize As Long = my_ibinaryreader.BaseStream.Length

            Dim partcount As Integer = ofilesize \ 104857600

            If ofilesize Mod 104857600 <> 0 Then partcount += 1

            my_ibinaryreader.BaseStream.Position = currentPartNumber * 104857600
            Dim myblocksize As Long = 0
            Dim my_bytearray() As Byte

            If currentPartNumber = partcount - 1 Then
                myblocksize = ofilesize - (currentPartNumber * 104857600)
                myblocksize = 104857600
            End If

            my_bytearray = my_ibinaryreader.ReadBytes(myblocksize)
            Return my_bytearray

        Catch ex As Exception
            Return Nothing
        End Try

What I have tried:

I tried to get file as one part (420 MB) but still getting different data size and file being corrupt on client side
Updated 18-Jul-19 5:31am

1 solution

WCF is most likely encoding the returned data as either XML or JSON. You need to tell it to return the raw binary data.

For example:
Public Function GetFilePart(ByVal currentPartNumber As Integer) As Stream
    OutgoingResponse.ContentType = "application/octet-stream"
    Dim blockSize As Integer = 104857600
    Dim offset As Long = currentPartNumber * blockSize
    Using inputStream As Stream = File.OpenRead(File)
        inputStream.Seek(offset, SeekOrigin.Begin)
        Dim buffer() As Byte = New Byte(blockSize - 1) { }
        Dim count As Integer = inputStream.Read(buffer, 0, buffer.Length)
        Return New MemoryStream(buffer, 0, count)
    End Using
End Function
See the bottom of this article[^] for more details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900