简体   繁体   English

客户端接收到的字节数组大小与WCF服务发送的字节数组大小不同

[英]The size of the bytearray received to the client is different from that sent from the WCF service

I have a Self Hosted WCF service which is reading a big file with binaryreader and returning bytearray to client我有一个自托管 WCF 服务,它正在使用 binaryreader 读取一个大文件并将字节数组返回给客户端

Service splitting file as parts and 10MB per parts( 10485860 Bytes)服务拆分文件作为部分和每部分 10MB(10485860 字节)

But client received 13981237 Bytes per parts, why is this difference.但是客户端每部分收到 13981237 字节,为什么会有这种差异。

This making my file is corrupt这使我的文件已损坏

This is my code from WCF这是我来自 WCF 的代码

 myblocksize = 10485860
 my_bytearray = my_ibinaryreader.ReadBytes(myblocksize)
 my_ibinaryreader.Close()
 Return my_bytearray

and This is my client code这是我的客户端代码

dim myWebClient as new WebClient
bytearray = myWebClient.DownloadData("SERVICEURL?file=bla.rar&currentPartNumber=myPartNumber")

and this is my WCF service full code这是我的 WCF 服务完整代码

  Dim i As Integer = 0


        my_InStream = New System.IO.FileStream("d:\temp\" + 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 \ 10485860

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


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

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

        my_bytearray = my_ibinaryreader.ReadBytes(myblocksize)
        my_ibinaryreader.Close()
        my_InStream.Close()
        Return my_bytearray

@AlessandroMandelli writing bytearray into file with below code @AlessandroMandelli 使用以下代码将字节数组写入文件

First getting filesize from WCF that (example: 425000000 Byte - 435 MB)首先从 WCF 获取文件大小(例如:425000000 字节 - 435 MB)

and calculating partcount with filesize\\104857600 (43 parts example)并使用 filesize\\104857600 计算零件数(43 个零件示例)

and getting all parts with for statement并使用 for 语句获取所有部分

in WCF seeking by currentpart * 104857600在 WCF 中按 currentpart * 104857600 查找

please check below code请检查下面的代码

dim myfileStream as filestream = new Filestream ("C:\aa.rar", filemode.create)

 dim partcount as integer = filesize \ 104857600

dim myWebClient as new WebClient

  for i as integer = 0 to partcount -1

       bytearray = myWebClient.DownloadData("SERVICEURL?file=bla.rar&currentPartNumber=myPartNumber")

    if not isnothing(byteArray) then

     myfileStream.Write(byteArray, 0 , byteArray.length)

     endif

next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM