怎么用vb判断文件是否上传完毕?
当别人用FTP上传文件给我时,我要用vb判断:文件是否上传完成?(要考虑传输过程中网络断开等原因。)当文件上传完成后,提示我处理文件。问题是:怎么用vb判断文件是否上传完毕? --------------------编程问答-------------------- 对上传文件进行重命名操作,如能改名,那么上传完毕。如不能,表示正在上传。 --------------------编程问答-------------------- 重命名使用Shell调用DOS命令:ReName --------------------编程问答-------------------- 所以你看到ftp服务器上传者一般都会在上传完所有的文件后再放一个 complete (或者类似文件名)的空文件。 --------------------编程问答--------------------
当网络断开的时候,文件没有传完也可以改名。 --------------------编程问答-------------------- 传完再上传一个设定的文件 --------------------编程问答-------------------- ftp的没高过,规法下文件格式,固定个文件尾。 --------------------编程问答-------------------- 向高手们学习了! --------------------编程问答--------------------
'可以试试独占打开文件,如果打开成功,说明上传完毕。--------------------编程问答--------------------
function iscompleted(filename as string) as boolean
dim f as integer
on error goto openfailed
f=freefile()
open filename for binary lock read write as #f
Close #f
iscompleted=true
exit function
openfailed:
iscompleted=false
end function
当文件没传完,但是网络断开的时候,文件可以打开成功。 --------------------编程问答-------------------- 是自己写的 ftp server 就可以分得那么细,如果不是,很难确保数据的完整性 --------------------编程问答-------------------- 可否判断文件大小是否一致来判别ftp已经传输完成? --------------------编程问答--------------------
怎么判断文件大小是否一致? --------------------编程问答-------------------- ++ --------------------编程问答-------------------- 没有具体测试:
--------------------编程问答-------------------- 如果你不知道文件大小,是不好判断是否上传完成。
' --- module1.bas ---
Option Explicit
' Async Upload
Private Type ConnectionSettings
Server As String
Username As String
Password As String
Port As Long
End Type
Private Type UploadSettings
RemoteFile As String
LocalFile As String
End Type
Private Declare Function InternetOpenA Lib "Wininet.dll" (ByVal lpszAgent As Long, ByVal dwAccessType As Long, ByVal lpszProxyName As Long, ByVal lpszProxyBypass As Long, ByVal dwFlags As Long) As Long
Private Declare Function InternetSetStatusCallback Lib "Wininet.dll" (ByVal hInternet As Long, ByVal lpfnInternetCallback As Long) As Long
Private Declare Function InternetConnectA Lib "Wininet.dll" (ByVal hConnect As Long, ByVal lpszServerName As String, ByVal nServerPort As Long, ByVal lpszUsername As String, ByVal lpszPassword As String, ByVal dwService As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "Wininet.dll" (ByVal hInternet As Long) As Long
Private Declare Function FtpPutFileA Lib "Wininet.dll" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32" (xDest As Any, xSource As Any, ByVal nBytes As Long)
Private Const FTP_TRANSFER_TYPE_UNKNOWN As Long = &H0
Private Const FTP_PASSIVE As Long = &H8000000
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
Private Const RequestSent As Long = 31
Private Const ConnectionClosed As Long = 51
Public Connect As ConnectionSettings
Public Upload As UploadSettings
Public bSent As Double ' 4KB byte chunks
Public bLast As Double ' last bytes sent
Public bTotal As Double ' Total bytes uploaded
Public Function IsWhole(ByVal FileLength As Long) As Boolean
FileLength = (FileLength / 4096)
If (FileLength And 1) = 0 Then
IsWhole = True
Else
IsWhole = False
End If
End Function
Public Sub UploadFile()
Dim mOpen As Long
Dim mConn As Long
Dim mHandle As Long
mOpen = InternetOpenA(0&, 1, 0&, 0&, 1)
Call InternetSetStatusCallback(mOpen, AddressOf INTERNET_STATUS_CALLBACK)
mConn = InternetConnectA(mOpen, _
Connect.Server, _
Connect.Port, _
Connect.Username, _
Connect.Password, _
1, FTP_PASSIVE, 2)
Call FtpPutFileA(mConn, _
Upload.LocalFile, _
Upload.RemoteFile, _
FTP_TRANSFER_TYPE_UNKNOWN, 2)
Call InternetCloseHandle(mConn)
Call InternetCloseHandle(mOpen)
End Sub
Public Sub INTERNET_STATUS_CALLBACK( _
ByVal hInternet As Long, _
ByVal dwContext As Long, _
ByVal dwInternetStatus As Long, _
ByVal lpvInfo As Long, _
ByVal dwInfoLength As Long)
Dim cbRead As Long
Select Case dwInternetStatus
Case RequestSent
RtlMoveMemory cbRead&, ByVal lpvInfo&, dwInfoLength
If cbRead = 4096 Then
bSent = bSent + cbRead
Else
bLast = cbRead
End If
DoEvents
End Select
End Sub
' --- Form1 ---
' Add 1 command button
Option Explicit
Private Sub Command1_Click()
Dim CheckSum As Long
CheckSum = 0
With Connect
.Server = "ftp.server.com"
.Username = "user"
.Password = "password"
.Port = 21
End With
With Upload
.LocalFile = "C:.exe"
.RemoteFile = "notepad.exe"
End With
CheckSum = FileLen(Upload.LocalFile)
Call UploadFile
If IsWhole(CheckSum) Then
bTotal = bSent
Else
bTotal = (bSent + bLast)
End If
If CheckSum = bTotal Then
MsgBox "checksum OK" _
& vbNewLine & "FileSize= " & CheckSum _
& vbNewLine & " Bytes uploaded= " & bTotal
End If
End Sub
因为你的VB程序不是FTP,不是你接收的。
--------------------编程问答-------------------- 使用Internet Transfer 控件?
如果是:
不要使用OpenURl方法,使用execute方法,然后监控StateChanged 事件
State 的设置值:
常数 值 描述
icNone 0 无状态可报告。
icHostResolvingHost 1 该控件正在查询所指定的主机的 IP 地址。
icHostResolved 2 该控件已成功地找到所指定的主机的 IP 地址。
icConnecting 3 该控件正在与主机连接。
icConnected 4 该控件已与主机连接成功。
icRequesting 5 该控件正在向主机发送请求。
icRequestSent 6 该控件发送请求已成功。
icReceivingResponse 7 该控件正在接收主机的响应。
icResponseReceived 8 该控件已成功地接收到主机的响应。
icDisconnecting 9 该控件正在解除与主机的连接。
icDisconnected 10 该控件已成功地与主机解除了连接。
icError 11 与主机通讯时出现了错误。
icResponseCompleted 12 该请求已经完成,并且所有数据均已接收到。
--------------------编程问答--------------------
别人用Internet Transfer控件传输过来,我来检测。
补充:VB , 网络编程