读取2进制文件后放在字节数组里不就可以了?
我当时也遇到过这种问题,我就是用了lilltetao的方法,将文件先以二进制方式打开,然后Read到一个数组,再用senddata发送,getdata接收后再write到一个二进制文件中,但是用此方法传送一个位图文件后不成功,文件头有错误(好象与空格有关系)。我觉的此种方法在理论上是可行的,多调试几次应该能成功
littletao is right,
也许你应该先定义要发送字符数组的大小:首先用filelen函数确定要发送文件大小,
然后用redim重新定义字符数组的大小,ok。
只要允许字节数组,还有什么不能干的?
1月31日《计算机世界报》上有一篇文章,是delphi的,但可参考。
PutFile 用来传送文件,数据流的格式是:第一个字节代表传送的是文件(因为在我的程序中我还要传送确认,用这个字节以示区分);第二个字节代表文件名长度,以后是文件名、数据块长度、数据块。
Dim bRecieved As Boolean
Private Sub tcpServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim byteData() As Byte, rsResult As ADODB.Recordset
If bytesTotal > 0 Then
ReDim byteData(bytesTotal - 1)
tcpServer(Index).GetData byteData, vbByte + vbArray
Select Case byteData(0)
Case 0
bRecieved = True
Case 1
SaveFile byteData
tcpServer(Index).SendData CByte(0)
End Select
End If
End Sub
Private Sub PutFile(objSendSock As Winsock, sFileName As String)
Dim iThisFile As Integer, byteData() As Byte
Dim i As Long, j As Long, byteBuffer() As Byte
Dim iBlockCount As Integer, lDivValue As Long
' On Error GoTo errPutFile
iThisFile = FreeFile
Open sFileName For Binary As #iThisFile
ReDim byteData(Len(sFileName) + 3)
byteData(0) = 1
byteData(1) = CByte(Len(sFileName))
For j = 1 To Len(sFileName)
byteData(j + 1) = CByte(Asc(Mid(sFileName, j, 1)))
Next
iBlockCount = LOF(iThisFile) \ BufferSize
For i = 0 To 1
byteData(i + 2 + Len(sFileName)) = CByte((iBlockCount \ 2 ^ (i * 8)) And &HFF)
Next
For i = 1 To iBlockCount
ReDim Preserve byteData(BufferSize + 10 + Len(sFileName))
For j = 0 To 1 '计算当前帧号
byteData(j + 4 + Len(sFileName)) = CByte((i \ 2 ^ (j * 8)) And &HFF)
Next
For j = 0 To 3 '计算数据块长度
byteData(j + 6 + Len(sFileName)) = CByte((BufferSize \ 2 ^ (j * 8)) And &HFF)
Next
ReDim byteBuffer(BufferSize - 1)
Get #iThisFile, , byteBuffer
For j = 0 To BufferSize - 1
byteData(j + 10 + Len(sFileName)) = byteBuffer(j)
Next
bRecieved = False
objSendSock.SendData byteData
Do While Not bRecieved
DoEvents
Loop
Next
lDivValue = LOF(iThisFile) Mod BufferSize
If lDivValue <> 0 Then
ReDim Preserve byteData(lDivValue + 10 + Len(sFileName))
For j = 0 To 1
byteData(j + 4 + Len(sFileName)) = CByte((i \ 2 ^ (j * 8)) And &HFF)
Next
For j = 0 To 3
byteData(j + 6 + Len(sFileName)) = CByte((lDivValue \ 2 ^ (j * 8)) And &HFF)
Next
ReDim byteBuffer(lDivValue - 1)
Get #iThisFile, , byteBuffer
For j = 0 To lDivValue - 1
byteData(j + 10 + Len(sFileName)) = byteBuffer(j)
Next
bRecieved = False
objSendSock.SendData byteData
Do While Not bRecieved
DoEvents
Loop
End If
Close #iThisFile
Exit Sub
errPutFile:
Select Case Err.Number
Case Else
MsgBox Err.Number & ":" & Err.Description, vbInformation, "PutFile 错误提示"
End Select
End Sub
Private Sub SaveFile(byteData() As Byte)
Dim lSize As Long, i As Integer, strTemp As String
Dim sFileName As String, byteBuffer() As Byte
Dim iThisFile As Integer, lSoFarIn As Long
Dim iDotPos As Integer
Dim iBlockIndex As Integer, iBlockCount As Integer
'On Error GoTo errSaveData
sFileName = ""
For i = 1 To byteData(1)
sFileName = sFileName & Chr(byteData(i + 1))
Next
For i = 0 To 1
iBlockCount = iBlockCount + byteData(i + 2 + byteData(1)) * 2 ^ (i * 8)
Next
For i = 0 To 1
iBlockIndex = iBlockIndex + byteData(i + 4 + byteData(1)) * 2 ^ (i * 8)
Next
If iBlockIndex = 1 Then
i = 0
strTemp = sFileName
Do While Dir(strTemp) <> ""
i = i + 1
strTemp = sFileName
iDotPos = InStr(strTemp, ".")
strTemp = Left(strTemp, iDotPos - 1) & "_" & i & Right(strTemp, Len(strTemp) - iDotPos + 1)
Loop
If Dir(sFileName) <> "" Then
FileCopy sFileName, strTemp
Kill sFileName
End If
End If
For i = 0 To 3
lSize = lSize + byteData(i + byteData(1) + 6) * 2 ^ (i * 8)
Next
ReDim byteBuffer(lSize - 1)
For i = 0 To lSize - 1
byteBuffer(i) = byteData(i + byteData(1) + 10)
Next
iThisFile = FreeFile
Open sFileName For Binary As iThisFile
lSoFarIn = LOF(iThisFile) + 1
Put #iThisFile, lSoFarIn, byteBuffer
Close #iThisFile
Exit Sub
errSaveData:
Select Case Err.Number
Case Else
MsgBox Err.Number & ":" & Err.Description, , "SaveFile 错误提示"
End Select
End Sub
对不起,我忘了定义BufferSize了,BufferSize不能太大
Const BufferSize = 2 * 1024
关注..