㈠ 如何用VB網路傳輸圖片
使用以下的函數,可以傳送任何文件.
至於顯示,你只要打開傳送完得到的文件就行了,這應該沒問題吧?
Const Max As Long = 65534 '每次最大傳送數據
Dim SendPos As Double '發送數據位置
Dim RecPos As Double '接收數據位置(此二變數可實現斷點續傳)
'傳送文件
Sub SendFile(FileName As String, Wnk As Winsock)
'FileName 預發送的文件.
Static iPoss As Double '當前發送位置
Dim SendData() As Byte '二進制數據
Dim Length As Double '記錄文件長度
Dim FileNum As Integer
FileNum = FreeFile '獲得文件號
Length = FileLen(FileName) '獲得文件長度
Open FileName For Binary As FileNum
DoEvents
If Length <= Max Then
ReDim SendData(1 To Length)
Get FileNum, , SendData
Wnk.SendData SendData
Else
While iPos <= Length - Max
ReDim SendData(1 To Max)
Get FileNum, iPos + 1, SendData
Wnk.SendData SendData
iPos = iPos + Max
Wend
End If
ReDim SendData(Length - iPos - 1) '此處注意要-1,否則不會成功!
Get FileNum, iPos + 1, SendData
Wnk.SendData SendData
Close FileNum
Debug.Print FileLen(FileName)
End Sub
'==============================================================
'==============================================================
Sub ReceiveData(FileName As String, Wnk As Winsock, Lens As Long) '接收數據
'FileName 文件保存的位置
Dim RecData() As Byte
Dim Length As Double
Dim FileNum As Integer
FileNum = FreeFile
Open FileName For Binary As FileNum
Length = FileLen(FileName)
ReDim RecData(1 To Lens)
Wnk.GetData RecData
Put FileNum, Length + 1, RecData
Close FileNum
End Sub