下面是传送文件的代码
怎么把文件名及格式同时传过去?
谁做过类似的东西
给个思路
有代码更好
多谢!!!‘上传文件***************************************************************************
Private Sub cmdSend_Click()
'向服务器发送文件
Dim myFile() As Byte
Dim lngFile As Long '文件长度
Dim FileName As String '文件名称
Static i As Single
sockClient.Close
sockClient.Connect
‘检查是否连接
Do
DoEvents
If sockClient.State = sckConnected Then
Exit Do
Else
i = i + 1
If i > 200000 Then ‘连接超时
Dim box As String
box = MsgBox("是否放弃?", vbYesNo, "连接失败:")
If box = vbYes Then
i = 0
Exit Do
Else
i = 0
End If
End If
End If
DoEvents
Loop
lenth = 0
'检查是否与服务器连接
If sockClient.State = sckConnected Then
FileName = VB.App.Path & "\1.jpg" '取得文件名及路径
lngFile = FileLen(FileName) '取得文件长度
ReDim myFile(lngFile - 1) As Byte '初始化数组
Open FileName For Binary As #1 '打开文件
Get #1, , myFile '将文件写入数组
Close #1 '关闭文件
sockClient.SendData myFile '发送
Else
MsgBox "没有连接"
End If
End SubPrivate Sub sockServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
'接收文件
Static i As Long
Dim myFile() As Byte
Dim myLong As Double
Dim myPath As String
myPath = VB.App.Path & "\a.jpg"
ReDim myFile(0 To bytesTotal - 1)
sockServer(Index).GetData myFile
Open myPath For Binary As #1 '新建文件
Put #1, lenth + 1, myFile '将收到的数据写入新文件中
Close #1 '关闭
'记录文件长度
lenth = lenth + UBound(myFile) - LBound(myFile) + 1
End Sub
‘上传文件***************************************************************************
怎么把文件名及格式同时传过去?
谁做过类似的东西
给个思路
有代码更好
多谢!!!‘上传文件***************************************************************************
Private Sub cmdSend_Click()
'向服务器发送文件
Dim myFile() As Byte
Dim lngFile As Long '文件长度
Dim FileName As String '文件名称
Static i As Single
sockClient.Close
sockClient.Connect
‘检查是否连接
Do
DoEvents
If sockClient.State = sckConnected Then
Exit Do
Else
i = i + 1
If i > 200000 Then ‘连接超时
Dim box As String
box = MsgBox("是否放弃?", vbYesNo, "连接失败:")
If box = vbYes Then
i = 0
Exit Do
Else
i = 0
End If
End If
End If
DoEvents
Loop
lenth = 0
'检查是否与服务器连接
If sockClient.State = sckConnected Then
FileName = VB.App.Path & "\1.jpg" '取得文件名及路径
lngFile = FileLen(FileName) '取得文件长度
ReDim myFile(lngFile - 1) As Byte '初始化数组
Open FileName For Binary As #1 '打开文件
Get #1, , myFile '将文件写入数组
Close #1 '关闭文件
sockClient.SendData myFile '发送
Else
MsgBox "没有连接"
End If
End SubPrivate Sub sockServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
'接收文件
Static i As Long
Dim myFile() As Byte
Dim myLong As Double
Dim myPath As String
myPath = VB.App.Path & "\a.jpg"
ReDim myFile(0 To bytesTotal - 1)
sockServer(Index).GetData myFile
Open myPath For Binary As #1 '新建文件
Put #1, lenth + 1, myFile '将收到的数据写入新文件中
Close #1 '关闭
'记录文件长度
lenth = lenth + UBound(myFile) - LBound(myFile) + 1
End Sub
‘上传文件***************************************************************************
或者换这里http://www.5ivb.net/down/5ivb_6_1.asp自己搜sock就可以了~
发来看看吧
多谢
有TCP的吗?
Public Sub SendFile(sckSend As Winsock, fileName As String)
Dim hIn, fileLength, ret, hOut
Dim temp() As Byte
Dim Send As Long
Dim Block As Long
Block = Blocksize
hIn = FreeFile
On Error GoTo ErrorHandler
Open fileName For Binary Access Read Shared As hIn
DoEvents
fileLength = LOF(hIn)
Rn = ""
Send = 0
Do While Not EOF(hIn)
On Error GoTo ErrorHandler
If fileLength - Loc(hIn) <= Block Then
Block = fileLength - Loc(hIn)
End If
If Block = 0 Then Exit Do
ReDim temp(Block - 1)
Get hIn, , temp '// Read a block of data
sckSend.SendData temp '// Off it goes
Send = Send + UBound(temp) + 1
On Error Resume Next
DoEvents
Sleep 10
Loop
Close hIn Exit Sub
ErrorHandler:
Close hIn
SendFile = False
End Sub
sckFile.GetData temp
Put #fileNum, , temp
Dim fileLength As Long, sizeOfFileSent As Long, sizeOfFile As Long On Error GoTo ErrorHandler
DoEvents ProgressBar1.Value = FileLen(App.Path & "\abc.exe") Exit SubErrorHandler:
MsgBox "An error occured while saving " & CommonDialog1.FileTitle & ". File Transfer being canceled.", vbOKOnly, "IO Error"
cancel_Click
End Sub