将文件转换为字节,发送它并将其从字节数组转换回文件

时间:2015-08-04 04:18:58

标签: vb.net tcp bytearray

我试图通过字节数组在vb.net中通过tcp连接发送文件。

如何将字节数组转换回文件并使用savefiledialog保存?

我用来将文件转换为字节数组并发送它的代码是:

Private Sub btnfSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfSend.Click
    browseFile.Title = "Browse Files"
    browseFile.InitialDirectory = "C:\Users\LF\Desktop"
    browseFile.ShowDialog()
End Sub

Private Sub browseFile_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles browseFile.FileOk
    If ((lblStatus.Text = "Status: Receiving") Or (lblStatus.Text = "Status: Connected")) Then
        Dim strm As System.IO.Stream
        strm = browseFile.OpenFile()
        Dim fileDir As String
        fileDir = browseFile.FileName.ToString()
        Dim fInfo As New FileInfo(fileDir)
        Dim numBytes As Long = fInfo.Length

        Dim fStream As New FileStream(fileDir, FileMode.Open, FileAccess.Read)
        Dim br As New BinaryReader(fStream)
        Dim data As Byte() = br.ReadBytes(CInt(numBytes))
        ChatSocket.SendTo(data, SocketFlags.None, New IPEndPoint(IPAddress.Parse(txtRemoteIP.Text), CInt(Val(txtRemotePort.Text))))
        br.Close()
        fStream.Close()
    End If
End Sub

0 个答案:

没有答案