1,502
社区成员
发帖
与我相关
我的任务
分享
Public Sub UploadFile()
Dim nUpFile As Integer
nUpFile = Me.ListView1.ListItems.Count
Dim nIndex As Integer
For nIndex = 1 To nUpFile
Me.lblNo = nIndex 'Me.ListView1.ListItems(nIndex).SubItems(0)
Me.lblLocalPath = Me.ListView1.ListItems(nIndex).SubItems(1)
Me.lblFileSize = Me.ListView1.ListItems(nIndex).SubItems(2)
Dim strFileExt As String
strFileExt = FileSystemObject.GetFileExt(Me.lblLocalPath)
'请求上传,要求服务器配文件名
Dim bSendData() As Byte
bSendData() = StrConv(WinSockInfo.C2S_REQUEST_UPLOADFILE & WinSockInfo.SAC_CONNECT_STRING & strFileExt, vbFromUnicode)
MainStart.Winsock1.SendData bSendData()
DoEvents
Next nIndex
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim bData() As Byte
Call Me.Winsock1.GetData(bData(), vbByte, bytesTotal)
Dim strData As String
strData = StrConv(bData(), vbUnicode)
'Call Me.Winsock1.GetData(strData, , bytesTotal)
'检验是否为可识别格式
Dim nPos As Integer
nPos = InStr(strData, WinSockInfo.SAC_CONNECT_STRING)
If nPos <= 0 Then
Debug.Print "收到不明数据"
Exit Sub
End If
Dim strEventID As String
strID = Left(strData, nPos - 1)
Dim strTemp As String
strTemp = Right(strData, Len(strData) - nPos)
Select Case CInt(strID)
Case WinSockInfo.S2C_UPLOADFILE_NAME '服务器分配好上传文件名,客户端开始上传文件
frmUpload.lblRomoteFile.Caption = strTemp
'开始上传文件
Dim nSendSize As Long
Dim nFileNo As Integer
nFileNo = FreeFile
Dim bValue() As Byte
Open frmUpload.lblLocalPath For Binary Access Read As nFileNo
Dim bSendData() As Byte
Dim nFileSize As Long
nFileSize = FileLen(frmUpload.lblLocalPath)
Dim nReadSize As Integer
nReadSize = 8 * 1024 - 4 '因VB的SendData每次只能发送8K字节,否则要分包发送。同时这里的数据发送需要前后增加标记,故需减4
Dim nLastReadSize As Integer '最后一次读取字节数
nLastReadSize = nFileSize Mod nReadSize
'计算需要发送多少次
Dim nReadIndex As Long
Dim nReadCount As Long
nReadCount = nFileSize \ nReadSize
If nLastReadSize <> 0 Then
nReadCount = nReadCount + 1
End If
Dim strSendHead As String
strSendHead = WinSockInfo.C2S_UPLOADFILE & WinSockInfo.SAC_CONNECT_STRING
Dim strSendTail As String
Dim strSendData As String
For nReadIndex = 1 To nReadCount
If nReadIndex <> nReadCount Then
ReDim bValue(nReadSize - 1) As Byte
Get #nFileNo, , bValue()
Else
ReDim bValue(nLastReadSize - 1) As Byte
Get #nFileNo, , bValue()
End If
If EOF(nFileNo) = False Then
strSendTail = WinSockInfo.SAC_CONNECT_STRING & WinSockInfo.SAC_SEND_CONTINUE
Else
strSendTail = WinSockInfo.SAC_CONNECT_STRING & WinSockInfo.SAC_SEND_COMPLETE
End If
Dim strValue As String
strValue = StrConv(bValue(), vbUnicode)
strSendData = strSendHead & strValue & strSendTail
bSendData() = StrConv(strSendData, vbFromUnicode)
Me.Winsock1.SendData bSendData()
Next
Case WinSockInfo.S2C_UPLOADFILE_SIZE '服务器发来已收到文件的字节数
frmUpload.lblUpSize.Caption = strTemp
frmUpload.ProgressBar1.Value = (CLng(strTemp) * 1#) / frmUpload.lblFileSize * 100
If frmUpload.ProgressBar1.Value = 100 Then
frmUpload.Visible = False
Else
frmUpload.Visible = False
End If
Case Else
End Select
End Sub
Private Sub WinsockTransFile_DataArrival(Index As Integer, ByVal bytesTotal As Long)
'得到传送来的数据
Dim bGetData() As Byte
Me.WinsockTransFile(Index).GetData bGetData(), vbByte, bytesTotal
Dim strGetData As String
'Me.WinsockTransFile(Index).GetData strGetData, , bytesTotal
strGetData = StrConv(bGetData(), vbUnicode)
Dim strEvent As String
'检验是否为可识别格式
Dim nPos As Integer
nPos = InStr(strGetData, WinSockInfo.SAC_CONNECT_STRING)
Dim addWinsockInfo As WinSockInfo.WinSockInfo
Dim bSendData() As Byte
Dim strSendData As String
If nPos <= 0 Then
strEvent = "收到不明数据"
Else
Dim strEventID As String
strEventID = Left(strGetData, nPos - 1)
Dim strTemp As String
strTemp = Right(strGetData, Len(strGetData) - nPos)
Select Case CInt(strEventID)
Case WinSockInfo.C2S_SEND_COM_AND_APP ' 客户端发送计算机名及程序名
strEvent = "收到程序名"
Dim strComputerName As String
Dim strAppName As String
Dim strCallBack As String
Dim nPos2 As Integer
nPos2 = InStr(strTemp, WinSockInfo.SAC_CONNECT_STRING)
If nPos2 <= 0 Then
strCallBack = "不明数据"
Else
strComputerName = Left(strTemp, nPos2 - 1)
strAppName = Right(strTemp, Len(strTemp) - nPos2)
Me.lblClientCompName(Index) = strComputerName
Me.lblClientAppName(Index) = strAppName
End If
Case WinSockInfo.C2S_REQUEST_UPLOADFILE '请求上传文件
'计算文件名称
'Dim strFileExt As String
strFileExt = strTemp
'Dim strSaveFileName As String
strSaveFileName = Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now) & Second(Now)
'判断是否已存在
Dim nNo As Integer
nNo = 0
Do While FileSystemObject.IsFileExisit(Me.Label2.Caption & "\" & strSaveFileName & "." & strFileExt) = True
nNo = nNo + 1
strSaveFileName = Left(strSaveFileName, 14) & nNo
Loop
'发送最终文件名给客户端,用于显示
strEvent = "客户请求上传"
strCallBack = "文件名:" & strSaveFileName & "." & strFileExt
addWinsockInfo.strID = Me.ListView1.ListItems.Count + 1
addWinsockInfo.strIP = Me.WinsockTransFile(Index).RemoteHostIP
addWinsockInfo.strEvent = strEvent
addWinsockInfo.strTime = Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now) & Second(Now)
addWinsockInfo.strName = Me.lblClientCompName(Index)
addWinsockInfo.strPort = Me.WinsockListenClient(Index).RemotePort
addWinsockInfo.strProgram = Me.lblClientAppName(Index)
addWinsockInfo.strCallBack = strCallBack
Call AddListView(addWinsockInfo)
strSendData = WinSockInfo.S2C_UPLOADFILE_NAME & WinSockInfo.SAC_CONNECT_STRING & strSaveFileName
bSendData() = StrConv(strSendData, vbFromUnicode)
Me.WinsockTransFile(Index).SendData bSendData()
Exit Sub
Case WinSockInfo.C2S_UPLOADFILE '上传文件,接受数据
'取得上传标记
Dim strFlag As String
strFlag = Right(strTemp, 2)
'接收数据
Dim strWriteData As String
strWriteData = Left(strTemp, Len(strTemp) - 2)
Dim bWriteData() As Byte
bWriteData() = StrConv(strWriteData, vbFromUnicode)
'写入文件
Dim nFileNo As Integer
nFileNo = FreeFile
Dim strSaveFilePath As String
strSaveFilePath = Me.Label2.Caption & "\" & strSaveFileName & "." & strFileExt
Open strSaveFilePath For Binary Access Write As #nFileNo
Put #nFileNo, , bWriteData()
Close #nFileNo
'发送已收到字节数
Dim nRecivedFileSize As Long
nRecivedFileSize = FileLen(strSaveFilePath)
strSendData = WinSockInfo.S2C_UPLOADFILE_SIZE & WinSockInfo.SAC_CONNECT_STRING & nRecivedFileSize
bSendData() = StrConv(strSendData, vbFromUnicode)
Me.WinsockTransFile(Index).SendData bSendData()
Case Else
strEvent = "收到不明数据"
End Select
End If
addWinsockInfo.strID = Me.ListView1.ListItems.Count + 1
addWinsockInfo.strIP = Me.WinsockTransFile(Index).RemoteHostIP
addWinsockInfo.strEvent = strEvent
addWinsockInfo.strTime = Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now) & Second(Now)
addWinsockInfo.strName = Me.lblClientCompName(Index)
addWinsockInfo.strPort = Me.WinsockListenClient(Index).RemotePort
addWinsockInfo.strProgram = Me.lblClientAppName(Index)
addWinsockInfo.strCallBack = strCallBack
Call AddListView(addWinsockInfo)
End Sub